c++ : replacing a char in a string
how can I replace a char in a given string with an other.
exemple string = "this is a string" replace all the "s" with "f" string = "thif if a ftring" thanx for help |
Code:
string convert(string str_in) |
It's better to use the standard C++ algorithms. For example:
Code:
#include <string> |
Code:
string convertTR(string str_in) I get an compiler error like this: Code:
main.cpp: In function `std::string convertTR(std::string)': |
You're incorrectly using replace(), see the link below for more help:
Good luck |
Specifically, the replace algorithm (defined in <algorithm>), has this (informal) prototype:
void replace (ForwardIterator beg, ForwardIterator end, const T& oldValue, const T& newValue) In practical terms, you give it the the beginning and end of a sequence to operate on, an original value to replace, and a new value to replace it with. In your code, the third parameter is a negative number, not a character. Not sure exactly what you were thinking that param was for. Anyway, since the algorithm is actually a template, the compiler is saying that it couldn't find any way to instantiate the template such that it would be an acceptable match for the way you tried to call the function. |
Quote:
|
Quote:
|
Gotta agree with lowpro2k3 here. For example, the parameter, -121, that you give in the first line doesn't correspond to and ASCII character I know. I believe ASCII is a 7-bit unsigned encoding, so only values 0-127 make sense.
|
#include <stdio.h>
char * chstrswithf(char * a) { while(*a!='\0') { if(*a=='s') *a='f'; a++; } return a; } int main(void) { char string[]="this is a string"; chstrswithf(string); printf("%s \n",string); return 0; } -- This code is released under GPL v2. http://www.gnu.org/copyleft/gpl.html |
The chstrswithf() example will work with a null-terminated C-string, but it is not a good idea to try it with a C++ STL string. The implementation is not guarunteed to store the string as a null-terminated buffer. For example, it is possible that it could be implemented to store a buffer and a size.
|
Quote:
'A' = 65 (dec), 0x41 (hex) 'a' = 97 (dec), 0x61 (hex) ' ' = 32 (dec), 0x20 (hex) There are no negative numbers in the ASCII chart. The normal ASCII range is 0-127, the extended range is 0-255 (unsigned byte). ASCII chart and Extended ASCII chart |
Quote:
Code:
void convert(string & str_in) |
well, I'm sorry but some of the characters that I want to replace aren't in the ascii table nor in the extended ascii table
I don't know if you are able to see these characters but here is the list. I can't find them in the table, and I'm pretty sure they are negative numbers because I tested the converter that I've used with normal characters, and they were just fine 'ð' ('g' with a line on it) 'þ' ('s' with a hook) 'ý' ('i' without the dot on it) I've found them in the ascii table 'ç' ('c' with a hook) 'ö' ('o' with double dots on it) 'ü' ('u' with double dots on it) These are Turkish characters, any idea?? thanks for help PS: Code:
|
Quote:
Code:
string str_in("ðþý"); the first place, you could set up a resource file where you could store these characters and read them in at the beginning of the program. I haven't tested this out because I don't have Turkish fonts, but it seems to me like it should work. |
All times are GMT -5. The time now is 10:34 AM. |