how do i increment a string array?
like a becomes b, x becomes y?
ex. Hello world becomes ifmmp xpsme.
Printable View
how do i increment a string array?
like a becomes b, x becomes y?
ex. Hello world becomes ifmmp xpsme.
An array of chars is actually an array of 1 byte numbers.
The ASCII table lists the character representations of these numbers. If you don't have that chart handy, just look for yourself:
char c = 0;
while(c < 127) {
printf("%c = %i \n", c, c);
getch();
c++;
}
So really, it's just a matter of looping through the string and incrementing the value of each char. That's basically it.
If you are using the STL, you can do it with std::string and the transform algorithm:
Code:#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
class IncrementString {
public:
inline char operator()(char c){ return ++c; }
};
int main()
{
string str("Hello World");
cout<<"String before transform: "<<str<<endl;
transform(str.begin(), str.end(), str.begin(), IncrementString());
cout<<"String after transform: "<<str<<endl;
return 0;
}
how about any word inputted in the c++ program? just any other word becomes all the letters in the word incremented