Thread: Converting underscores into spaces?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Converting underscores into spaces?

    How do I take all the underscores inside of a char, and then convert them into spaces. I don't want to print them out after, but rather replace that same variable with the new non-underscored phrase.

    for example, if I have

    char name = "I_love_you";

    I want to replace name with "I love you"
    sorry, im a newb, just trying to learn c on my own. thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for each character
        if character equals _
            this character = ' ';
    Give something like that a try.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    Code:
    char string[] = "I_like_peanuts!";
    char *p = string;
    
    for (; *p; ++p)
    {
            if (*p == '_')
                    *p = ' ';
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    thanks! I get it now. it that simple huh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  4. Replies: 5
    Last Post: 06-30-2003, 12:52 PM