LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-04-2005, 03:45 PM   #1
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Rep: Reputation: 30
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
 
Old 06-04-2005, 04:04 PM   #2
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30
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?

Last edited by cb951303; 06-04-2005 at 04:06 PM.
 
Old 06-04-2005, 04:50 PM   #3
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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.
 
Old 06-05-2005, 12:35 AM   #4
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30
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
 
Old 06-05-2005, 05:19 AM   #5
MoneyCat
LQ Newbie
 
Registered: Jun 2005
Location: USA
Distribution: Ubuntu 5.04
Posts: 10

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

Good luck

Last edited by MoneyCat; 06-05-2005 at 05:24 AM.
 
Old 06-05-2005, 01:04 PM   #6
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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.
 
Old 06-05-2005, 02:53 PM   #7
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30
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
 
Old 06-05-2005, 03:12 PM   #8
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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
 
Old 06-05-2005, 05:41 PM   #9
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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.
 
Old 06-05-2005, 06:13 PM   #10
llmmix
Member
 
Registered: Jun 2005
Posts: 73

Rep: Reputation: 15
#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

Last edited by llmmix; 06-05-2005 at 06:14 PM.
 
Old 06-05-2005, 08:08 PM   #11
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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.
 
Old 06-06-2005, 07:13 AM   #12
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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

Last edited by jtshaw; 06-06-2005 at 07:16 AM.
 
Old 06-06-2005, 08:54 AM   #13
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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.
 
Old 06-06-2005, 09:58 AM   #14
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30
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

Last edited by cb951303; 06-06-2005 at 10:14 AM.
 
Old 06-06-2005, 10:56 AM   #15
beforemath
LQ Newbie
 
Registered: Feb 2005
Posts: 8

Rep: Reputation: 0
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.
 
  


Reply

Tags
c++, replace, stl, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
prepend char to a string schneidz Programming 6 06-01-2005 08:53 AM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 06:25 PM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration