iostream.h is a deprecated C++ header file. conio.h is not a very portable library. If you can avoid it, and the getch() function, I would. Oh, and main() returns an int.
iostream.h is a deprecated C++ header file. conio.h is not a very portable library. If you can avoid it, and the getch() function, I would. Oh, and main() returns an int.
Ya i known it.
but i worte my program for bigginer, not for Pro.
It's not valid C nor is it valid C++, beginner or not.
You also won't be able to legally nor safely edit foo (it's a string literal)
foo is read-only as far as you're concerned. See MacGyver's post http://cboard.cprogramming.com/showp...2&postcount=14char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play";
That's as about as much help as you're getting for the solution, if you have specific problems - ask.Code:#include <stdio.h> int main(void) { char s[] = "Baltimore *MD*is*a*really*good*place*to*work*and*play"; /* Perhaps a loop could go here? That goes from 0 to strlen(s), or 0 to '\0' ? */ return 0; }
Last edited by zacs7; 08-22-2007 at 05:32 AM.
i was trying to post my code so i can get further help, i couldnt figure how to do that, help plz?
I imagine you might be having trouble using code tags.....
This:
[code]x = 1337;[/code]
Produces:
Code:x = 1337
insertCode:#include <string.h> int main (void) { 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 ); } return 0; }
ok so now do i get some help, i wrote code, can someone help me correct it?
yeah macgyver thats exactly what was going wrong when i was trying to post my code, thanks
* Read all the posts in this thread, in order. My last post explains why you can't do that (with foo) - it's a string literal, and strtok modifies the string (which is illegal in this case)
* strtok doesn't help.
* Include stdio.h
What don't you understand? You seem to be ignoring what people have been saying. Start at the begging of foo, go through it in a loop, if the current character is '*' then set it to ' ' until you get to the end of foo (ie '\0'). The lastly print the result.
sorry guys, i am complete newbie so taking very long to understand what exactly you guys saying, well i didnt mean to ignore, trust me i read each reply throughout and tried to understand it, i really appreciate it. right now i am working on writing a code the way you said i should, i will be back with that new code
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; }
hey zacs7, is this what you meant me to do? please tell me if there is anything wrong wiht itCode:#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; }
or do you think the way you have done it with strlen is better? as in making code short or long?
Last edited by hardik; 08-22-2007 at 07:10 AM.
It's almost correct.
* Remove foo & bar
* Try to be explicit with the loop, makes for a better read, ie.
If you want to keep the original as well as the modified, just make a buffer the same size as baz and write the contents to that (in the loop, char-by-char replacing * with space as you go) just don't forget the null terminator. Otherwise you're done, welldoneCode:while(*replace != '\0') { if(*replace == '*') *replace = ' '; /* and so on */
The easiest solution ever.
Code:#include <stdio.h> char text[]={"Charlotte *NC*is*a*really*cool*place*to*work*and*play"}; int main (int argc, char* argv[]){ int i; printf ("%s\n", text); for(i=0;text[i];i++){ if(text[i]=='*'){ text[i]=' '; } } printf ("%s\n", text); return 0; }
"The Internet treats censorship as damage and routes around it." - John Gilmore
i have a question for maxotator and zacs7, maxotator, in your code wont we need secondary buffer? same for zacs7 if i remove foo and bar wont i need a secondary buffer? i tried and got an error, also in my code i used strdup because strcpy strcpy will not allocate the memory for the copy for you, can i replace it with something more simple? how is the memory allocated in your code maxorator? oh btw i am asking to understand it not to critisize it, thank you both ofyou.
Last edited by hardik; 08-22-2007 at 10:59 AM.
here is rite code .
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]=' '; getch(); }