Strings

Hi I have an array that is the alphabet (in alphabetical order) and i wanted to know how move a string (that is inputed) one to the right in my array then output it
Thank you
Hi,

A char is just a small int, so you can increment it. Loop through your string, increment each char in it. You can use the [ ] operator just like an array to access each char. Might need an if statement if you want z to become a

Hope this helps :+)
How would i increment it
Use the ++ operator.

Can you show us your code, we can help from there :+)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    for( std::string alphabet : { "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ACDHIJKQRUVW" } )
    {
        std::cout << alphabet ;

        for( int i = 0 ; i < 3 ; ++i )
        {
            // http://en.cppreference.com/w/cpp/algorithm/rotate
            std::rotate( alphabet.begin(), alphabet.begin()+1, alphabet.end() ) ; // rotate by one to the left
            std::cout << " => " << alphabet ;
        }
        std::cout << '\n' ;

        std::cout << alphabet ;
        for( int i = 0 ; i < 3 ; ++i )
        {
            std::rotate( alphabet.rbegin(), alphabet.rbegin()+3, alphabet.rend() ) ; // rotate by three to the right
            std::cout << " => " << alphabet ;
        }
        std::cout << "\n\n" ;
    }
}

ABCDEFGHIJKLMNOPQRSTUVWXYZ => BCDEFGHIJKLMNOPQRSTUVWXYZA => CDEFGHIJKLMNOPQRSTUVWXYZAB => DEFGHIJKLMNOPQRSTUVWXYZABC
DEFGHIJKLMNOPQRSTUVWXYZABC => ABCDEFGHIJKLMNOPQRSTUVWXYZ => XYZABCDEFGHIJKLMNOPQRSTUVW => UVWXYZABCDEFGHIJKLMNOPQRST

ACDHIJKQRUVW => CDHIJKQRUVWA => DHIJKQRUVWAC => HIJKQRUVWACD
HIJKQRUVWACD => ACDHIJKQRUVW => UVWACDHIJKQR => KQRUVWACDHIJ

http://coliru.stacked-crooked.com/a/2d260aefa7385145

#include <iostream>
#include "string"
#include "array"
#include "stdio.h"
using namespace std;
string message;
char arrayLower[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

int main() {
int x;
string newMessage;
cout << "What will you like your coded message to say? (write everything in lower case)\n";

getline(cin, message);
for (x = 0; x < message.length(); x++) {
int linearSearch = 0;
char result;
result = linearSearch;
if (result == char()) {
while (message.length()) {
//this is where i want to increment
}
cout << "This is your new message " << newMessage;
}

else {
cout << "Please try again.";
}
}
return 0;

};
// this will check for chars
int linearSearch(){
long size = message.length();
for (char index = 0; index < size; index++) {
bool message(true);
if (message == arrayLower[index])
return index;

}
return 0;
};
Could you give an example?
For instance, if the user enters "hello world" as the message, what should the new message be?
if you input "hello world" it would output "ifmmp xpsmd" (one to the right)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    const std::string alphabet         = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
    const std::string shifted_alphabet = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza" ;

    const std::string message = "Hello World!" ; // message entered by the user

    std::string new_message ;
    
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char c : message ) // for each character c in message
    {
        // find the position of the character in the alphabet
        // http://www.cplusplus.com/reference/string/string/find/
        const auto pos = alphabet.find(c) ;

        // if it was found, add the corresponding character in shifted_alphabet
        if( pos != std::string::npos ) new_message += shifted_alphabet[pos] ;

        else new_message += c ; // otherwise, add the character unchanged
    }

    // http://en.cppreference.com/w/cpp/io/manip/quoted (C++14)
    std::cout << "    message: " << std::quoted(message) << '\n'
              << "new message: " << std::quoted(new_message) << '\n' ;
}

http://rextester.com/KITJ85044
Topic archived. No new replies allowed.