C++ Programming Solutions – Encrypt String

Today we will give you the C++ Programming Solution of Encrypting a String. There are many types of encrypting a string some of them are HERE. We are going to encrypt a simple String in C++. The Coding Problem and its Solution are below.
C++ Programming
C++ Programming

Question

Kathy is a scientist who is developing a machine that creates 5-character long string code. She prefers to encrypt the strings rather than save them as they are. She’s requested you to create software that would accept a 5-character string as input and encrypt it in the format she’s provided

You must look for the letters in the odd locations of the string and, if they are vowels, you must replace them with  _ (underscore).

Example
  • “hecod” should be “h_c_d”.
  • “progr” should be “progr”. because odd locations  are not vowels.

C++ Programming Solution

  • Starting Code in C++ will be
#include <iostream>
using namespace std;

int main()
{
return 0;
}

  • Let’s take input from the user
#include <iostream>
using namespace std;

int main()
{
string input = "";
cin>>input;
}
  • If the length of the input is not 5, input is wrong, and take input again.
#include <iostream>
using namespace std;

int main()
{
string input = "";
cin>>input;

while(input.length() != 5)
       {
       cout<<"Input is wrong Input Again"<<endl;
       cin>>input;
       }

}
  • Create a string containing only vowels
#include <iostream>
using namespace std;

int main()
{
string input = "";
cin>>input;
string vowels = "aeiouAEIOU";
while(input.length() != 5)
     {
     cout<<"Input is wrong Input Again"<<endl;
     cin>>input;
     }

}
  • If the Odd Positions Contain Vowels Replace it with UnderScore _.
#include <iostream>
using namespace std;

int main()
{
string input = "";
cout<<"Input :   ";
cin>>input;
string vowels = "aeiouAEIOU";
while(input.length() != 5)
     {
     cout<<"Input is wrong Input Again"<<endl;
     cin>>input;
     }
for(int i = 0; i < vowels.length(); i++)
    {
       if(input[1] == vowels[i])
            {
                input[1] = '_';
            }
      if(input[3] == vowels[i] )
          { 
              input[3] = '_';
         }

    }   

   cout<<"Modified String  ";
   cout<<input<<endl;
}

Output

Input: rAgUr
Modified String r_g_r

Input: hecodesit
Input is wrong Input Again
aeiou
Modified String a_i_u

READ MORE

C++ related posts Visit HERE

Python-related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge