replace the "*" in the above string with " ", a single space character using ANSI C??

Discussion in 'C' started by hardik, Aug 22, 2007.

  1. hardik

    hardik New Member

    Joined:
    Aug 22, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    ok, I am very new to C programming and i have a problem at hand, i wuld really really appreciate any help plz,



    The string is defined as the following:

    char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play";

    Using the smallest amount of code possible, replace the "*" in the above string with " ", a single space character using ANSI C language. The resulting string should be printed out at the end of the program. In addition to a manual examination, the resultant .C file will be compiled with GCC compiler with the "-ansi" flag set and executed to determine success

    CAN SOMEBODY HELP PLEASE?
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    We'd be glad to help, however we don't do your work for you. Refer to your lessons, work out some come, and present it for help with any problems you encounter. Be sure to read the "Before you make a query" thread (upper right corner of this page) before you post code -- code snippets should be inside code tags. You can learn about them there.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You need to be looping through each member and if some character match is found you need to be replacing it with the new character.
     
  4. listendinesh

    listendinesh New Member

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    You can also use standered function strtok( ) .Take a char pointer and allocate it memory enough to hold the string.call strtok for tokens and append space at the last of each token and copy first token to string then concadinate others.
     
  5. listendinesh

    listendinesh New Member

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    also put terminating char in the last of string.
     
  6. hardik

    hardik New Member

    Joined:
    Aug 22, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    ok here is the code i made

    Code:
    #include <stning.h>
    char *foo = " Charlotte *NC*is*a*really*cool*place*to*work*and*play";
    {
         char delims[]="*";
         char *result = NULL;
         result = strtok ( str, delims );
         while ( result != NULL ) 
         {
               printf ("result is "\%s"\n", result );
               result = strtok (NULL, delims );
         }
    }
    can some one help me improve it?
     
    Last edited by a moderator: Aug 22, 2007
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    He does not want to tokenize the string, he want to replace occurences of one character with other. There's no point in doing it in an unwieldy and inefficient way.

    On most operating systems the current string, as defined, wil be const. That is, he will not be able to write to it. This will require copying the string, while replacing the characters on the fly. The terminating zero is already there, it just needs to be included in the copy.
     
  8. hardik

    hardik New Member

    Joined:
    Aug 22, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
     
    Last edited by a moderator: Aug 22, 2007
  9. hardik

    hardik New Member

    Joined:
    Aug 22, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
     
    Last edited by a moderator: Aug 22, 2007
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    We normally don't solve people's homework for them, Hardik. We help THEM solve it. It is how one learns. I'm making an exception in this case because you are complicating the problem unduly, and doing the original poster a huge disfavor.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char* argv[])
    {
        // If your OS makes a string defined as follows a const value (most do)
        char *foo = "Charlotte *NC*is*a*really*cool*place*to*work*and*play";
        // then you'll have to make a copy of the string, thusly:
        char *bar = strdup (foo);
        // or you may just define the string differently, thusly:
        char baz [] = "Charlotte *NC*is*a*really*cool*place*to*work*and*play";
    
        // In any even, you make the substitution like this:
        char *replace = baz;
        printf ("%s\n", baz);
        while (*replace)
        {
            if (*replace == '*') *replace = ' ';
            replace++;
        }
    
        printf ("%s\n", baz);
    	return 0;
    }
    
     
  11. hardik

    hardik New Member

    Joined:
    Aug 22, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    well, if it makes feel good at all, this problem is not a homework problem, nor i am a student at all, also i see how simply you have solved it when i spent almost half night reading about strtok and delims, i missed a simple thing of copying string and making a simple loop, i guess i complicated it too much, :( but in anycase your help is not used in some classroom to get a better grade, thank you very much for ur directions
     
  12. hardik

    hardik New Member

    Joined:
    Aug 22, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    i have a question though, i knw strcpy will not allocate the memory for the copy for you so u used strdup, but is there an easier way to it without strdup? i was just wondering
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice