LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ : replacing a char in a string (https://www.linuxquestions.org/questions/programming-9/c-replacing-a-char-in-a-string-330369/)

cb951303 06-04-2005 03:45 PM

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

cb951303 06-04-2005 04:04 PM

Code:

string convert(string str_in)
{
    for(int i = 0; i < str_in.length(); i++)
    {
          switch(str_in[i])
          {
              case 's':
              str_in[i] = 'f';
              break;
          }
    }
   
    return str_in;
}

Is there a better way?

carl.waldbieser 06-04-2005 04:50 PM

It's better to use the standard C++ algorithms. For example:

Code:

#include <string>
#include <iostream>
#include <algorithm>

int main()
{
  using std::string;
  using std::cout;
  using std::endl;
  using std::replace;

  string s("Hejjo Worjd!");

  replace(s.begin(), s.end(), 'j', 'l');

  cout << s << endl;

  return 0;
}

If you want to replace more than one letter at a go, something like the standard algorithm, transform, might be more appropriate.

cb951303 06-05-2005 12:35 AM

Code:

string convertTR(string str_in)
{
      replace(str_in.begin(), str_in.end(), -121, 'c');
      replace(str_in.begin(), str_in.end(), -89, 'g');
      replace(str_in.begin(), str_in.end(), -115, 'i');
      replace(str_in.begin(), str_in.end(), -108, 'o');
      replace(str_in.begin(), str_in.end(), -97, 's');
      replace(str_in.begin(), str_in.end(), -127, 'u');
     
      return str_in;
}

what am I doing wrong?

I get an compiler error like this:

Code:

main.cpp: In function `std::string convertTR(std::string)':
main.cpp:11: error: no matching function for call to `replace(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int, char)'
main.cpp:12: error: no matching function for call to `replace(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int, char)'
main.cpp:13: error: no matching function for call to `replace(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int, char)'
main.cpp:14: error: no matching function for call to `replace(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int, char)'
main.cpp:15: error: no matching function for call to `replace(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int, char)'

main.cpp:16: error: no matching function for call to `replace(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int, char)'

make: *** [main.o] Error 1
Execution terminated

thanks for help

MoneyCat 06-05-2005 05:19 AM

You're incorrectly using replace(), see the link below for more help:

Good luck

carl.waldbieser 06-05-2005 01:04 PM

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.

cb951303 06-05-2005 02:53 PM

Quote:

Originally posted by carl.waldbieser
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.

its a ascii code and it's acceptable because there isn't a difference between 'a' and 25

lowpro2k3 06-05-2005 03:12 PM

Quote:

Originally posted by cb951303
its a ascii code and it's acceptable because there isn't a difference between 'a' and 25
But your using negative numbers which don't correspond to an ASCII value... What are you trying to replace, your code doesnt make sense to me :confused:

carl.waldbieser 06-05-2005 05:41 PM

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.

llmmix 06-05-2005 06:13 PM

#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

carl.waldbieser 06-05-2005 08:08 PM

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.

jtshaw 06-06-2005 07:13 AM

Quote:

Originally posted by cb951303
its a ascii code and it's acceptable because there isn't a difference between 'a' and 25
In case there is confusion here (which there seams to be)

'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

lowpro2k3 06-06-2005 08:54 AM

Quote:

Originally posted by cb951303
Code:

string convert(string str_in)
{
    for(int i = 0; i < str_in.length(); i++)
    {
          switch(str_in[i])
          {
              case 's':
              str_in[i] = 'f';
              break;
          }
    }
   
    return str_in;
}

Is there a better way?

Also you're using the function argument as a return value - no need, you can pass by reference instead in that situation.

Code:

void convert(string & str_in)
{
    // it seems from your switch case you want to swap 's' with 'f'
    replace(str_in.begin(), str_in.end(), 's', 'f');
    ...
}

I've never really used the replace() function, but depending on the return codes of it, you might want to check that in your function and return an int instead of void.

cb951303 06-06-2005 09:58 AM

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:


string convertTR(string str_in)
{
      for(int i = 0; i < str_in.length(); i++)
      {
              switch(str_in[i])
              {
                                case -121:
                                str_in[i] = 'c';
                                break;
                               
                                case -89:
                                str_in[i] = 'g';
                                break;
                               
                                case -115:
                                str_in[i] = 'i';
                                break;
                               
                                case -108:
                                str_in[i] = 'o';
                                break;
                               
                                case -97:
                                str_in[i] = 's';
                                break;
                               
                                case -127:
                                str_in[i] = 'u';
                                break;
              }
      }
     
      return str_in;
}

If you're not convinced that negative numbers work, try this pls and see yourself

beforemath 06-06-2005 10:56 AM

Quote:

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??
Can't you just assign the value to a char and use it as a replacement value?

Code:

string str_in("ðþý");
char gvar, svar, ivar;

gvar = str_in[0];
svar = str_in[1];
ivar = str_in[2];

replace(str_in.begin(), str_in.end(), gvar, 'g');
replace(str_in.begin(), str_in.end(), svar, 'g');
replace(str_in.begin(), str_in.end(), ivar, 'g');

I understand that you might not be able to enter the values in the compiler, but since you are reading them from file 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 12:41 AM.