ok using
#inlcude %26lt;iostream%26gt;
and
#include %26lt;cstring%26gt;
im wanting to make a program that takes a string you enter and replaces certain characters int he string with spesific other characters
like an ecryptor, but not for that perpous
now i got so far as to have the user enter the string with cin.getline
but i dont know how to have the program read the string and act on it
p.s. im willing to go throgh a tedious proccess of listing the characters and there replacements so all i need to know is how to read the string, and replace spesific characters with other spesific characters
ok?
ok.
=]
C++ string editing?
Code to iterate thru the string and replace all the
character a with character b
string::iterator It = str.begin();
while ( It != str.end() )
{
if ( *It == ' a' )
*It = 'b';
}
cout%26lt;%26lt;str
Hope this help
Reply:Using character strings (declared like you have, such as char str[N]; or char* str;) makes dipping into the string to deal with individual characters easy, because your string is really an array. So the first letter is at str[0], the second is str[1], and so on. So let's say I wanted to search through my string and replace the letter a with the letter B (and capitalization does matter!). I would write:
int i = 0, n = 5000;
char str[5000];
.
.
Read in string
.
.
while (i %26lt; n) {
if ( str[i] == 'a' ) str[i] = 'B'; //Note that I used single quotes
i++; //i = i+1;
}
So if my string was "Apple's are very great!" to start with, it would end up being "Apple's Bre very greBt!".
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment