how do I shift a strings characters over 1 (a -> b)

so I'm having troubles trying to shift my string that the user inputs over by a random number which is generated between 1-10 (for example: input: coffee random number: 3 output: friihh) but I don't know how to move stringed characters. I also need to be able to randomize the letters given (example: input: coffee output: ocefoc) any ideas?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>


/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

int main(int argc, char** argv) 
{
	string inputWord, inputReverse, inputShift;
	char indiChar;
	int stringSize;
	int shift;
	shift = 0;
	srand(time(0));
	
	cout << "\nHow many characters do you want to use in yor word?\n ";	
	cin >> stringSize;
	cout << "\nEnter a word: ";
	cin >> inputWord;
	cout << inputWord.at(stringSize - stringSize) << endl;
	cout << inputWord.at(stringSize - stringSize + 2) << endl;
	cout << inputWord.size() << endl;
	cout << "-------------------------------------------------------------------------" << endl;
	cout << "Reverse Order: " << endl;
	inputReverse = inputWord;
	inputReverse = string(inputReverse.rbegin(), inputReverse.rend());
	cout << inputReverse <<endl;
	cout << "-------------------------------------------------------------------------" << endl;
	inputShift = inputWord;
	shift = rand() % 10 +1;
	cout << shift << " postion shifted from " << inputWord << ": " << endl;
	if (shift <= 10)
	{
		//(int)inputShift + shift
	}
	cout << inputShift;
	
	system("pause");
	return 0;
}
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#include <iomanip>

char shift_char( char c, std::size_t offset )
{
    static const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                        "abcdefghijklmnopqrstuvwxyz" ;

    // locate c in the alphabet
    // https://en.cppreference.com/w/cpp/string/basic_string/find    
    const auto pos = alphabet.find(c) ;
    if( pos == std::string::npos ) return c ; // not found, return unchanged

    // now we want the character in the alphabet as position pos+offset
    // there is a small detail to be taken care of: pos+offset might be out of range
    // if we get to the end of the alphabet, we loop back to the beginning of the string
    // and continue counting from there onwards
    // note: modulo division result is in range [ 0, alphabet.size()-1 ]
    return alphabet[ (pos+offset) % alphabet.size() ] ;
}

std::string shift( std::string str, std::size_t offset )
{
    // shift each character in string str
    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( char& c : str ) c = shift_char(c,offset) ;

    return str ;
}

// shuffle the characters randomly
std::string shuffle( std::string str )
{
    // random number generator to use for shuffling
    // https://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ;

    // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle( str.begin(), str.end(), rng ) ;
    return str ;
}

int main()
{
    const std::string str = "Coffee Table!" ;
    std::cout << "original: " << std::quoted(str) << '\n' ;

    const std::size_t offset = 3 ;
    const std::string shifted_str = shift( str, offset ) ;
    std::cout << " shifted: " << std::quoted(shifted_str) << '\n' ;

    const std::string shuffled_str = shuffle(str) ;
    std::cout << "shuffled: " << std::quoted(shuffled_str) << '\n' ;
}

http://coliru.stacked-crooked.com/a/a1de00bfc197240c
what if i had to use a while loop and a at()? to get the random letters? I think that's what these are for but I don't know how to use it.
1
2
3
cout << inputWord.at(stringSize - stringSize) << endl;
cout << inputWord.at(stringSize - stringSize + 2) << endl;
cout << inputWord.size() << endl;
Topic archived. No new replies allowed.