hi, I have searched all kind of collate algorithms, but havent found nothing suitable for that kind of operation. Could somebody help? How is it possible to make distinctions of commas and other charackters?
hi, I have searched all kind of collate algorithms, but havent found nothing suitable for that kind of operation. Could somebody help? How is it possible to make distinctions of commas and other charackters?
Look around the string.h library for solutions. I know there are a couple of notable functions that find stuff in C-style strings, like strchr(). From there it's merely just changing whatever the function returns to an !
Why don't you just go through all the string and check each character ?Code:char string[] = "This, mister, is a message."; for(int i = 0; string[i] != '\0'; i++) if(string[i] == ',') string[i] = '!';
ok, but what does thatOriginally Posted by Desolation
part do?Code:!= '\0'
But Desolation is implying that my earlier solution (using a standard library function) is difficult. It is not.Code:string[i] != '\0' /* true if character i in string is not a terminating '\0' */
Code:#include <stdio.h> #include <string.h> int main (void) { char string[] = "hey, this is a simple message,,"; char *pch = NULL; while (pch = strchr(string, ',')) *pch = '!'; puts(string); return 0; }
Last edited by whiteflags; 05-23-2006 at 09:29 AM.
at first I tried in this way as Desolation told and it seems to work, but i have one small problem: How should the output(sentence where commas are replaced with exclamation marks) look like?
Code:#include <stdio.h> int main (void) { char string[] = "hey, this is a simple message,,"; for(int i = 0; string[i] != '\0'; i++) { if(string[i] == ',') { string[i] = '!'; } } printf ( */I woult like to have a sentence here, where commas are replaced with exclamation marks*/ ); getchar(); getchar(); return 0; }
Last edited by m4rtin; 05-23-2006 at 09:56 AM.
Uhm, use puts(string);
If you really insist on using printf, you can, even though you don't need it, but here's how.
I say you don't need printf because that prints out a format string, and you don't really need this type of work. Printing strings is already done better by other stdio functions like puts and fputsCode:printf("%*s\n", strlen(string), string);
Last edited by whiteflags; 05-23-2006 at 09:59 AM.
most basic question I've seen this weak, and there have been
quite a few...
printf ("%s", string);
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
ofcourse Thanx for your support!!
Just as an aside, you shouldn't use %s by itself. Dave pointed this out to me when he posted this article.printf ("%s", string);
http://en.wikipedia.org/wiki/Format_string_attack
It's that you shouldn't do this:Code:printf(str);
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.*