LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-06-2005, 12:19 PM   #16
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30

Quote:
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:

Code:
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, 02:09 PM   #17
beforemath
LQ Newbie
 
Registered: Feb 2005
Posts: 8

Rep: Reputation: 0
A bit of a question:
It seems that when I perform explicit conversion
Code:
(int)random_char
I get the ascii value for that character.
When I try
Code:
(char)random_int
I get the character for that ascii value.

Even this seems to also be allowed (or at least, as far as I can tell):
Code:
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, 05:48 PM   #18
carl.waldbieser
Member
 
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:

Code:
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

Code:
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).

Code:
#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, 01:11 PM   #19
harrylloyd2310
LQ Newbie
 
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled
Solution

Quote:
Originally Posted by cb951303 View Post
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
#include <iostream>
#include <string>
using namespace std;
// Replace function..
string replace(string word, string target, string replacement){
int len, loop=0;
string nword="", let;
len=word.length();
len--;
while(loop<=len){
let=word.substr(loop, 1);
if(let==target){
nword=nword+replacement;
}else{
nword=nword+let;
}
loop++;
}
return nword;

}
//Main..
int main() {
string word;
cout<<"Enter Word: ";
cin>>word;
cout<<replace(word, "x", "y")<<endl;
return 0;
}
 
Old 02-25-2015, 02:13 PM   #20
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
use this http://www.boost.org/doc/libs/1_57_0...g_algo.replace
 
Old 02-25-2015, 08:18 PM   #21
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,627

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
harrylloyd2310

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

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
 
  


Reply

Tags
c++, replace, stl, string



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration