sorry this one is rite.
Code:#include <stdio.h> #include <string.h> char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play"; int main(void){ for (int i=0 ; i<strlen(foo) ; i++) if (foo[i]=='*') foo[i]=' '; return 0; }
sorry this one is rite.
Code:#include <stdio.h> #include <string.h> char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play"; int main(void){ for (int i=0 ; i<strlen(foo) ; i++) if (foo[i]=='*') foo[i]=' '; return 0; }
try running it.
Lol, what's wrong with mine?
"The Internet treats censorship as damage and routes around it." - John Gilmore
Nothing, it's better than obaid's in the sense that using strlen as part of a for-loop end-check is a bad thing - especially when the string is being modified inside the loop, as the compiler may not realize if that the length is constant, and thus call strlen EVERY single iteration of the loop.
--
Mats
obaid's code is modifying a string literal too which are constant.
Why are there no standard restrictions on modifying string literals?
In trivial cases, it would be possible for the compiler to detect it, but in anything but trivial cases (e.g. when a function takes a char * as a parameter, and that char * happens to be a pointer to a literal).
It's not just obaid's code that does so, by the way, several of the other examples do too. Maxorators doesn't - but it also doesn't match the original poster's question - and by the way, we haven't seen any post from the OP for a while.... Perhaps the lesson ended? :-)
--
Mats
The string is stored on the stack (as an array of characters), we step through the original string, changing each * for a space, therefore modifying the original string - meaning no 2nd buffer is required, but if you wanted to... you can store the modified string in another buffer don't copy it at the start, do it as you check for '*' (since it's redundant - you'll be looping twice without knowing it).
OP is back hehe, well my problem was solved, so BIG THANKS TO ALL YOU GUYS OUT THERE HELPING OTHERS SO MUCH, but also just for sake of learning i am still keeping up with all your posting its interesting how quick one can learn frm a forum like this rather than going thru some biggg lessons and still geting clueless. btw i found maxorator's code most simple and short, but hey smiley i would love if you can explain more throughout that in which case it would not work?
Last edited by hardik; 08-22-2007 at 07:16 PM.
well maxotator, your code gives correct result but it changes the requirement, i think i would have to use the secondary buffer no matter what, as zacs said i am trying to remove foo and bar, and use char buf[100] as a secondary buffer, i will post back if i succeed, if anyone else can plz let me knowCode:#include <stdio.h> #include <string.h> int main (int argc, char* argv[]) { char *foo = "Charlotte *NC*is*a*really*cool*place*to*work*and*play"; char *bar = strdup (foo); char baz [] = "Charlotte *NC*is*a*really*cool*place*to*work*and*play"; char *replace = baz; printf ("%s\n", baz); while (*replace) { if (*replace == '*') *replace = ' '; replace++; } printf ("%s\n", baz); return 0; }
Okay this is as close as you'll get...
Now it's up to you to complete it and trim it down, it can be written in less code (ie with less variables and less statements... I've merely shown you how to step through foo char-by-char nothing more, nothing less.Code:#include <stdio.h> #include <string.h> int main(void) { char foo[] = " Charlotte *NC*is*a*really*cool*place*to*work*and*play"; size_t i = 0, len = strlen(foo); for(i = 0; i < len; i++) { /* foo[i] contains the current character, what should I do with it!?!? */ } /* I should probably print foo here... */ return 0; }
well wont i have to store foo in secondary buffer first? since i cant move/edit original one? what else can i use besides strdup to ease the process?
You can edit it if it's an array and not a pointer to the string literal. Because it's an array in this last example, you're fine in altering it. strdup()...... mmmm..... no.
Just ignore obaid for now. As everyone has been saying, he's trying to modify a string-literal, which is naughty and illegal, he also recalculates the string's size on every iteration - which is slow, as matsp said.i tried running it, unsuccessful attempt tho
check it out guys, how u like it?Code:#include<stdio.h> #include<string.h> int main() { char *foo = "Charlotte*NC*is*a*really*cool*place*to*work*and*play"; char buf[100]; int l = strlen(foo); int i; for(i=0;i<l;i++) { if(*foo=='*') buf[i] = ' '; else buf[i] = *foo; *foo++; } for(i=0;i<l;i++) printf("%c",buf[i]); printf("\n"); getchar (); }