Thread: Wats wrong!!

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01:52 PM
  4. wats wrong with this code?
    By Marrah_janine in forum C++ Programming
    Replies: 20
    Last Post: 12-10-2006, 08:01 PM
  5. wats happening around me.
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-17-2002, 12:59 PM