Wats wrong!!

This is a discussion on Wats wrong!! within the C Programming forums, part of the General Programming Boards category; Following is a simple code which is supposed to remove all vowels from the supplied string, which is not more ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Wats wrong!!

    Following is a simple code which is supposed to remove all vowels from the supplied string, which is not more than 80 bytes. But it is removing few other alphabets other than vowels. Can anyone tell me wat's wrong??
    Could '\b' not be used to for storing a backspace..

    Code:
    void main()
    {
    	char *p,*t;
    	t=p=(char *)malloc(80);
    	
    	gets(p);
    	while(*p)
    	{
    		   if(*p=='A'|| *p=='E' || *p=='I' || *p=='O' || *p=='U'||  *p=='a' || *p=='e' || *p=='i' || *p=='o' || *p=='u') 
    			*p='\b';
    		p++;
    	}
    
    	puts(t);
    	getch();
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    If you aren't going to make a copy of the string, and it looks like you aren't, then you should try overwriting the vowels you find, or something else. Using \b is on the wrong track. \b is a special character that only moves the cursor back once, it doesn't mean backspace. The backspace you use everyday is actually programmed in to work like that.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I note that instead of removing vowels, you replace them with '\b'.

    A few other things to note:
    • Include the appropriate headers.
    • The return type of main is int, not void.
    • There is no need to cast the return value of malloc.
    • Do not use gets. Use fgets or some other safer alternative.
    • free what you malloc
    • There probably is no need to use getch.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    \b does not really remove anything from your string. It is a terminal escape code, which alters the way a string is shown in terminal. It does not however remove any characters from your string. If you want to remove a character, then you need to actually move all other characters backwards to replace the character that is removed. Your assignment just got a bit more complicated

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Is that what the OS (or any basic word or notepad program) does when we hit 'Delete' or 'Backspace', or does it perform some other procedure?
    Last edited by juice; 09-26-2011 at 03:46 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by juice
    Is that what the OS does when we hit 'Delete' or 'Bacspace', or does it perform some other procedure
    It performs some other procedure that does not primarily involve removing characters from a string. Basically, you're on the wrong track if you're talking about the OS. Just think in terms of the algorithm at hand.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want to shift all the subsequent letters up to overwrite each vowel in the string?

    If so, don't use a replacement char , do it "low level", by actually shifting the char's, in the array, in the program.

    Hint: With each vowel you find, the subsequent shift becomes one char larger: ++gap.

    I mean, after the first vowel, you need to shift the subsequent char's by one char, in the string - but after the second vowel you find, you need to shift the subsequent char's, by two chars. With every vowel you find, the gap you have to shift, becomes one char larger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can any1 tell me wats wrong in this c program..
    By Hrisav in forum C Programming
    Replies: 2
    Last Post: 07-09-2009, 05:03 AM
  2. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  3. wats the fault...
    By ElemenT.usha in forum C Programming
    Replies: 17
    Last Post: 01-04-2008, 12:52 PM
  4. wats wrong with this code?
    By Marrah_janine in forum C++ Programming
    Replies: 20
    Last Post: 12-10-2006, 07:01 PM
  5. wats happening around me.
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-17-2002, 11:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21