Say I have a string of "10, 5, 3" How can I get rid of the commas so the string is just "10 5 3"? Should I be using strtok?
相关问题
- Multiple sockets for clients to connect to
- how to split a list into a given number of sub-lis
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Generate string from integer with arbitrary base i
Here is the program:
For detailed description you can refer this blog: http://tutorialsschool.com/c-programming/c-programs/remove-comma-from-string.php
Create a new string with the same size (+1 for the terminating character) as your current string, copy each character one by one and replace ',' by ' '.
In a
for
loop you would have something like this :Then after the
for
loop, do not forget to add '\0' at the end ofnew_string
.How about something like this? (My C is slightly rustyish and I don't have a compiler handy, so pardon any syntax bloopers)
You could also use a different variable to store the results, but since the result string is guaranteed to be equal or lesser length than the source string, it should be safe to overwrite it as we go.
A minor simplification to @melpomene.
Do potential assignment first and then check for the null character.