#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Arrow Remove vowels from string


    I would like to remove all vowels (except the first letter) of a user inputted string. I have written the following code to do so:

    Code:
    void removevowels( char *name ) { 
       int i, j; 	
       char vowels[6] = { 'A', 'E', 'I', 'O', 'U', '\0' }; 	
       for( i=1; i < strlen(name); i++ ) { 		
           if ( strchr(vowels, name[i]) ) { 			
               for ( j=i; j < strlen(name); j++ ) { 	
                    name[j] = name[j+1];	 	
               }	 		
            }  	
        } 
    }
    
    int main ( void ) {
        char inputname[25]; 
        printf("Insert Name:\n");
        scanf("%s", inputname);  
        removevowels(inputname);
        printf("No vowels: %s\n", inputname);
        return 0;
    }
    Which works in the case where you enter "WILLI" and it returns "WLL" (i.e. removing the I's). However, as soon as another vowel is in the name, problems occur.

    e.g. Entering "WILLIAMS" returns "WLLAMS" (just removing I's) instead of the expected "WLLMS" (removing I's and A's).

    Where have I gone wrong?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    After scrunching the remainder of the string to the left when you remove a vowel you need to check the character that just moved into that same position.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void removevowels( char *name ) {
      int i, j;
      char vowels[6] = { 'A', 'E', 'I', 'O', 'U', '\0' };
      for( i=1; i < strlen(name); i++ )
        if ( strchr(vowels, name[i]) ) {
          for ( j=i; j < strlen(name); j++ )
    	name[j] = name[j+1];
          --i; /****************************HERE*/
        }
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Are you required to use this approach? Another approach would be to send the char array by reference. You could build a temporary character string and add the characters from the original that aren't vowels and then reset the parameter to the new array.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by SGilligan
    I would like to remove all vowels (except the first letter) of a user inputted string. I have written the following code to do so:

    Code:
    void removevowels( char *name ) { 
       int i, j; 	
       char vowels[6] = { 'A', 'E', 'I', 'O', 'U', '\0' }; 	
       for( i=1; i < strlen(name); i++ ) { 		
           if ( strchr(vowels, name[i]) ) { 			
               for ( j=i; j < strlen(name); j++ ) { 	
                    name[j] = name[j+1];	 	
               }	 		
            }  	
        } 
    }
    
    int main ( void ) {
        char inputname[25]; 
        printf("Insert Name:\n");
        scanf("%s", inputname);  
        removevowels(inputname);
        printf("No vowels: %s\n", inputname);
        return 0;
    }
    Which works in the case where you enter "WILLI" and it returns "WLL" (i.e. removing the I's). However, as soon as another vowel is in the name, problems occur.

    e.g. Entering "WILLIAMS" returns "WLLAMS" (just removing I's) instead of the expected "WLLMS" (removing I's and A's).

    Where have I gone wrong?
    Another thing to notice in your code is that the condition is outer for loop may be different each time because the string may be smaller.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Great, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo