Incrementing and decrementing strings of letters

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
#include <bits/stdc++.h>
using namespace std;

int main ()
{
    int n;
    string let;

    cin >> n;
    cin >> let;

    vector<char> v;

    for (int i = 0; let[i] != '\0'; i++)
        v.push_back(let[i]);

    int p = 0;

    for (int t = 0; t < v.size(); t++)
    {
        for (int a = 0; a < n; a++)
            v[t] ++;

        for (int p = 0; v[t] > 'z'; p++)
            v[t] = v[t] - 1;
        v[t] -= p;
    }

    for (int a = 0; a < v.size(); a++)
    {
        cout << v[a];
    }

    return 0;
}


If the letter reaches z, it is supposed to start decrementing itself, so I think I have found a way to do it. However when I try it, instead of printing out the correct result, it prints out something different. How can I fix this code?
Example:
Input: 1 z
Output: y
Last edited on
1
2
3
string let; 
//...
cin >> let;

use std::getline() for std::string input to accommodate white-space
let[i] != '\0'
unlike a C-string a std::string is not null-terminated, so this condition might never be satisfied
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::string myString = "{Hellz World}";
    std::vector<char> myVec {myString.cbegin(), myString.cend()};
    for (auto& elem : myVec)std::cout << ( (elem > 'z') ? --elem : elem );
}
Can you fix the code so I can see what you have in mind?
line7: input string with some characters > z in the ASCII table
line8: initialize std::vector<char> with this input string
//uses the 4th version of the std::vector<> ctor as shown here: http://en.cppreference.com/w/cpp/container/vector/vector
line9: print individual elements of the std::vector<char> depending on whether it is >z (in which case it's decreased by 1) or not (in which case it's left unchanged). this line also uses the ternary operator within std::cout:
https://stackoverflow.com/questions/9619424/conditional-operator-used-in-cout-statement
That still doesn't help, I need you to modify the code so that it works, as for the site the first one gives out a problem with the page so I think it is the incorrect link, I can't open it.
Both of the site links work for me.
Registered users can post here. Sign in or register to post.