arrays - C: Program to delete characters other than alphabetical -


so i'm trying create program looks @ string defined in main, , deletes non-alphabetical characters (excluding \0). far code:

/* write code considers string saved  * in 'name' array, removes spaces , non-alphabetical  * chars string, , makes alphabetical characters  * lower case. */  #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h>  #define namelen 30  int main (void) {   char name[namelen];   strcpy(name, " william b. gates");     int i, length, check;      length = strlen(name);     ( = 0; < length; i++ ) {         check = isalpha(name[i]);         if ( check == 0 ) {             ( ; < length; i++ ) {                 name[i] = name[i+1];             }         }     }     printf("the length %lu.\n", strlen(name));    printf("name after compression: %s\n", name);   return exit_success; } 

so test data, " william b. gates", output should "williambgates", unfortunately output i'm getting is:

the length 16. name after compression: william b. gates 

i think space before william has been deleted, i'm unable tell. help!

you don't need complicated double-loop at all. purpose of exercise maintain independent source-reader , destination-writer, copying , advancing latter when former qualified criteria (i.e. answers true isalpha).

in other words:

#include <stdio.h> #include <ctype.h>  int main (void) {     char name[] = " william b. gates";     char *dst = name, *src;      (src = name; *src; ++src)     {         if (isalpha((unsigned char)*src))             *dst++ = *src;     }     *dst = 0; // terminate string      printf("result: %s\n", name); } 

output

result: williambgates 

i leave translating lower case during copy-step exercise you. (from in-code comment: "makes alphabetical characters lower case").


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -