2 registered members (AndrewAMD, TipmyPip),
12,420
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Whats wrong with this?
#140369
07/10/07 17:19
07/10/07 17:19
|
Joined: Jul 2006
Posts: 783 London, UK
sheefo
OP
User
|
OP
User
Joined: Jul 2006
Posts: 783
London, UK
|
I cannot figure out why this code doesn't work. It should replace every 's' character with a 'f' character. It manages to replace once then terminates the string directly after. Example: "This is a test string!" becomes "Thif", when it should be "Thif if a teft ftring!". Code:
char* chstrswithf(char* a) { while(*a != '\0') { if(*a == 's') *a = 'f'; a++; } return a; }
|
|
|
Re: Whats wrong with this?
[Re: Tobias]
#140372
07/11/07 17:55
07/11/07 17:55
|
Joined: Jul 2006
Posts: 783 London, UK
sheefo
OP
User
|
OP
User
Joined: Jul 2006
Posts: 783
London, UK
|
Code:
char string[32]; strcpy(string, "This is a test string!"); chstrswithf(string);
printf(string); // => "Thif"
|
|
|
Re: Whats wrong with this?
[Re: sheefo]
#140373
07/12/07 08:39
07/12/07 08:39
|
Joined: Jan 2007
Posts: 63 Oregon
mbartman
Junior Member
|
Junior Member
Joined: Jan 2007
Posts: 63
Oregon
|
Well I know how I would ordinarily do it with strings.. Code:
string chstrswithf(string a) { int temp_pos = 0; while(a[temp_pos] != '\0') { if(a[temp_pos] == 's') a[temp_pos] = 'f'; a++; } return a; }
BUT Lite-C doesn't seem to support indexing a string.. It's late though, so I hope you get this figured out.. Perhaps I will look at it again when I get up. Good luck,
Michael Bartman
A6/A7 Commercial Edition
Programmer By Nature
|
|
|
Re: Whats wrong with this?
[Re: jcl]
#140375
07/12/07 16:50
07/12/07 16:50
|
Joined: Jan 2007
Posts: 63 Oregon
mbartman
Junior Member
|
Junior Member
Joined: Jan 2007
Posts: 63
Oregon
|
You're right jcl, I shouldn't be coding that late at night, it's dangerous lol
Michael Bartman
A6/A7 Commercial Edition
Programmer By Nature
|
|
|
|