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

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    14

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

    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. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Since this appears to be homework, I won't give you a solution.

    What have you learned so far on how to go about it? Don't ask for a solution. Try it yourself and tell us where you're stuck.

    On a side note, I think the people that wrote the assignment need to get a clue about string literals and read-only memory.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Unhappy well its not a homework problem,

    schools r i guess not started yet summer is still going on, and i do apologize that i got frustrated about it and posted in all C forums, i need it for my work, i would apreciate if someone gets back with some help, oh also i can see in the problem they r using pointers and i am soooooo not good at it, so any help?
    Last edited by hardik; 08-21-2007 at 09:12 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It's an assignment of some sort. I doubt you have a job where the manager gives you a task like that, in such a pathetic manner. If it's work, consider quitting.

    So as I said before, what have you learned about C? Have you covered arrays, strings and such before?

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Unhappy yes, i did very basic level programming

    and i did learn arrays and strings, but nothing on this advance level, i would love if you can point me somewhere where i can learn it like very quick? i know you are not giving out just whole solution, but you can atleast tell me where do i go and learn it

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Duplicate threads deleted.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by hardik View Post
    and i did learn arrays and strings, but nothing on this advance level, i would love if you can point me somewhere where i can learn it like very quick? i know you are not giving out just whole solution, but you can atleast tell me where do i go and learn it
    If you know arrays, then why not walk through the string in a loop and change each char?

    Disclaimer: I know the string in this example should be considered read-only, so a copy should be done and all alterations on the string should be done to the copy, but if the teacher/professor/manager/whatever wants it done this way, then whatever. Stupid, but whatever.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    14
    well, its not a teacher/professor/manager. lolz i see u still firmly believe its for school assignment, i thought of it very first but then i saw its asking smallest amount of code possible, dont you think it will be too long?

  9. #9

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, then if it's not for school, what is it?

    Let me explain real quick what is wrong with what you got since it's still bothering me:

    Code:
    char *szHello1 = "Hello\n";
    char szHello2[] = "Hello\n";
    The first one is generally implemented into read-only memory on most systems even though I believe C makes no assumptions and allows you the opportunity to mess with the string. If you try to write to it, you may crash, you may not. What happens can't be guarenteed.

    The second case allows you to manipulate the data because it implicitly makes a copy. That means it copies the string from read-only memory to the array that is properly sized to receive the string.

    This is somewhat advanced, but you're asking how to overwrite the first string, which shouldn't be done. You need to either change the declaration to the second form or copy it over to a new buffer.

    So anyway, back to your problem, no I don't think it will be too long to do that. What's your alternative? You want to use some other function that looks through the string just like your program will do? Only thing I could suggest would be if you already know the spacing of the '*''s, then you could handle it faster than a straight linear search.... ie, if every other char was a '*' then you would search every other char and replace.

    For that specific string, if you want to be super fast, just change each position manually by index. You already know each index where a '*' is, so it's a simple assignment and can be done on one line.... ie. x = y = z = ' '; except you'd be doing it for each char of the string that has a '*'.

    How's that for a hint?

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    lying is also bad, just because it's summer there doesn't mean it's summer here. If you've covered arrays, strings and conditional loops then there is no reason why you wouldn't be able to do this simple not advanced task.

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    strchr, loops and pointer dereferencing are your friends

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why bother with strchr? You can do it in a simple loop with a condition...

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    indeed cleaner to do it like that

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    Wink Help But Not Solution.

    I won't give you a solution. but i wanna help u.
    MacGyver is Rite that "Do Your Own HomeWork".

    if you fellow these step, then you will write your program by own.

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    
    char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play";
    
    void main(){
    
    	1) write a loop, 
    	2) check where '*' is present in String 
    	3) then replaced by ' '.
    
    getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM