Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-06-2005, 01:19 PM   #16
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30

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.
actually I already tried that, the thing is I also used the replace function like this:

replace(str_in.begin(), str_in.end(), '', 'o')
but in this case if str_in's value is given in the source code, there is no problem but if I type it in the console (with cin), It doesn't work at all.

So what you're saying will surely work, but not with a input string :/
Old 06-06-2005, 03:09 PM   #17
LQ Newbie
Registered: Feb 2005
Posts: 8

Rep: Reputation: 0
A bit of a question:
It seems that when I perform explicit conversion
I get the ascii value for that character.
When I try
I get the character for that ascii value.

Even this seems to also be allowed (or at least, as far as I can tell):
replace(str_in.begin(), str_in.end(), (char)(-121), 'c');
replace(str_in.begin(), str_in.end(), (char)(-89), 'g');
replace(str_in.begin(), str_in.end(), (char)(-115), 'i');
Do simple conversions like that not work with expanded character sets at the command line, then?
Old 06-06-2005, 06:48 PM   #18
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
The problem is that for function templates, the template parameters are deduced by the compiler based on the types you are passing in as parameters. The replace function is really a template function. It looks like:

template <class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
So if you pass in iterator, iterator, int, char, it looks for the prototype

void replace(iterator, iterator, int, char)
Since the last two parameters are always the same type, the compiler can't find it, and it barks at you.

If you are going to be using characters outside the ASCII and extended ASCII set (i.e. characters that require more than 8 bits), you should probably consider using wstring instead of string. However, the global streams cin, cout, and cerr used for console input, output, and error are only capable of handling char data-- not wchar_t data.

Here is my previous example, tweaked to use a wstring instead of a string. This uses the *NIX device /dev/tty to write to the console, since I can't send a wide-character string to a cout (a character stream).

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

int main()
   using std::wstring;
   using std::wofstream;
   using std::endl;
   using std::replace;

   wstring s(L"Hejjo Worjd!");

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

   // Create a wide-character output stream to write to the console.
   wofstream wcout("/dev/tty");

   wcout << s << L'\n';

   return 0;
Old 02-25-2015, 02:11 PM   #19
LQ Newbie
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled

Originally Posted by cb951303 View Post
how can I replace a char in a given string with an other.


string = "this is a string"

replace all the "s" with "f"

string = "thif if a ftring"

thanx for help
#include <iostream>
#include <string>
using namespace std;
// Replace function..
string replace(string word, string target, string replacement){
int len, loop=0;
string nword="", let;
let=word.substr(loop, 1);
return nword;

int main() {
string word;
cout<<"Enter Word: ";
cout<<replace(word, "x", "y")<<endl;
return 0;
Old 02-25-2015, 09:18 PM   #21
John VV
LQ Muse
Registered: Aug 2005
Posts: 15,659

Rep: Reputation: Disabled

i take it you did not notice the date on this very old thread ?

yes this is a 10 year old post
for a version of gcc that is TWO FULL generations of computer hardware and software OUT OF DATE

and for a ten year old version of c++ standards


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 09:06 AM
How to convert string to char? twirl Programming 27 10-13-2005 08:11 AM
prepend char to a string schneidz Programming 6 06-01-2005 09:53 AM
Convert C++ string to C char* nyk Programming 3 06-17-2004 09:15 AM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 07:25 PM

All times are GMT -5. The time now is 10:26 AM.

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