special characters removing from srting

This is a discussion on special characters removing from srting within the C Programming forums, part of the General Programming Boards category; how can i ermove special characters from my string example: i havechar *str=" cprogramming\n"; how can i remove "\n". any ...

  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,047
    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: how to remove newline from a string
    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,211
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    that's why the first string should be declared as const char* in the first place
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 08: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, 03:49 PM
  4. Special characters
    By WarBaboon in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2003, 08:27 PM
  5. Special (non) Keyboard Characters
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 12:08 AM

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