How do I deleting invaid characters in a string?
eg. the user types in hello.daniel is there a way to delete the . in the middle of hello and daniel to make the string look like this "hellodaniel"
Thanks
How do I deleting invaid characters in a string?
eg. the user types in hello.daniel is there a way to delete the . in the middle of hello and daniel to make the string look like this "hellodaniel"
Thanks
OS:- XP
Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)
Sounds like a good exercise in string manipulation to me There isn't a standard function to do this for you, so you'll have to write your own.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Ok I will start trying to do that, but if anyone comes up with a way to do it can they please post it here
Thanks
OS:- XP
Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)
Pretty easy.
Code:#include <stdio.h> #include <string.h> void remove_char( char *string, const char item ) { char *at = strchr( string, item ); while( *at ) *at++ = *(at + 1); } int main() { char str[50] = "hello.daniel"; printf( "%s\n", str ); remove_char( str, '.' ); printf( "%s\n", str ); return 0; }
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6
Here's another that removes all occurances of the item instead of just the first one.
Code:void remove_char( char *string, const char item ) { char *at; while( (at = strchr( string, item )) ) while( *at ) *at++ = *(at + 1); }
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6
I was just about to ask how I would do that
Thank you to all who replied
OS:- XP
Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)
Ignore the first two functions I gave you, they're both big time wrong. This one's right.
Code:void remchr( char *string, const char item ) { char *at; while( (at = strchr( string, item )) ) { while( *at ) { *at = *(at + 1); at++; } } }
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6
Why were the last two functions "big time" wrong? They worked perctly fine for me!
OS:- XP
Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)
When I try to make this function remove a '\' from the string it doesn't work can some please help
thanks
OS:- XP
Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)
>> When I try to make this function remove a '\' from the string it doesn't work can some please help
Are u trying to remove a '\' ??
Shouldn't u be trying to remove a '\\' ??
Try it, maybe it'll work!
Yes that worked thanks.
OS:- XP
Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)
The big time problem was in this line.
It's undefined behavior because it changes at more than once in the same expression. It worked fine for me too, but that doesn't mean it'll work all the time or even any of the time. You just can't, tell, so it's wrong.Code:*at++ = *(at + 1);
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6