Thread: special characters removing from srting

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    special characters removing from srting

    how can i ermove special characters from my string
    example:
    i havechar *str=" cprogramming\n";
    how can i remove "\n".
    any function for removing special characters
    thank u
    sree

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    removing special characters from string

    how can i ermove special characters from my string
    example:
    i havechar *str=" cprogramming\n";
    how can i remove "\n".
    any function for removing special characters
    thank u
    sree

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cnu_sree View Post
    how can i ermove special characters from my string
    example:
    i havechar *str=" cprogramming\n";
    how can i remove "\n".
    any function for removing special characters
    thank u
    sree
    This is a nice function posted by Cactus Hugger, a while ago. It removes blank spaces from a string. I'll bet you could easily figure out how to alter this function to remove any ascii value you sent to the function as another parameter:

    Code:
    void remove_spaces(char *string)
    {
    	char *read, *write;
    
    	for(read = write = string; *read != '\0'; ++read)
    	{
    		/* edit: or if(!isspace(*read)), see swoopy's suggestion below */
    		if(*read != ' ')
    		{
    			*(write++) = *read;
    		}
    	}
    	*write = '\0';
    }
    If you get stuck, fear not - there are all kinds of ways of doing this function. Post up your problem when and if your function has errors you need help with.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're trying to remove the newline that is usually present in strings read by fgets(), there are many ways to do it. The favourite seems to be this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char line[BUFSIZ], *p;
    fgets(line, sizeof(line), stdin);
    if((p = strchr(line, '\n'))) *p = 0;
    Do a board search and you'll find many other ways. Here's another related thread: http://cboard.cprogramming.com/showthread.php?t=70320
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Warning.....

    Code:
    char *szString = "Some type of string.\n";
    Do not try to alter the above string. If you want szString to be altered, it should be declared like this:

    Code:
    char szString[] = "Some type of string.\n";
    Internally, the two forms are very different. One major difference is that the string could very well be in read-only memory, which can cause severe problems for you later on.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    that's why the first string should be declared as const char* in the first place
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Special characters and sending them to programs...
    By Dragoon_42 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2006, 04:49 PM
  4. Special characters
    By WarBaboon in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2003, 09:27 PM
  5. Special (non) Keyboard Characters
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 12:08 AM