Answers for "a1z26 code c++"

C++
0

a1z26 code c++

#include <iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
    string str1 = "", str2 = "";
    cout << "A1Z26 cipher generator." << endl;
    cout << "Generates a string of characters into numbers." << endl << endl;
    cout << "Enter the string (up to 100 characters, no numbers):\n";
    //Get input string.  Use newline as terminator and store the string in str1
    getline(cin,str1,'\n');
    //truncate str1 to 100 characters
    str1 = str1.substr(0,100);
    for(int i=0;i <str1.length();i++)
    {
        stringstream ss;
        string temp = "";
        if(isalpha(str1[i]) || str[i] == ' ')
        {
            //break up  the string with spaces in the same places.  This trim the '-' off the end before adding the space.
            if(str1[i] == ' ')
            {
                str2 = str2.substr(0, str2.length() -1) + str[i];
            }
            else
            {
                //convert the char to int and substract the offset
                //stringstream is an easy way to convert numbers to strings.
                ss << (int)(tolower(str1[i])-96);
                ss >> temp;                
                str2 += temp + "-";
            }
        }
    }
    //Trim the '-' off the final string
    str2= str2.substr(0, str2.length() -1);
    cout << "Your generated number: " << endl;
    cout << str2 << endl <<endl;
    cout << "Share this to your friends to decode your mystery!" << endl;
    system("pause");
    return 0;
}
Posted by: Guest on October-18-2021

Browse Popular Code Answers by Language