Write a C++ program that will ask the user for their name and a series of random
ID: 3863363 • Letter: W
Question
Write a C++ program that will ask the user for their name and a series of randomly input characters. The program will collect the string of random characters, reverse their order to the screen and place the user’s name in the middle of the string. You shall also count the number of special characters and display the count on the screen. For instance, if the user’s name was Big Bird and the list of random characters was #<4fr<g, the output should be
g<r Big Bird f4<#
There are 3 special characters. all programs ( unless otherwise noted) must prompt the user to rerun the program by typing "yes" or "Yes" . Upon exiting the program, you must “thank the user” for using it. (Programmed in C++)
Explanation / Answer
Answer:
Below is the required C++ code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name[20];
char rand_string[20];
int i;
int count=0;
int rand_pos = 0;
cout<<"Enter a random string::";
cin>>rand_string;
cout<<"Enter your name ::";
cin>>name;
for(i=0; i<strlen(rand_string); i++)
{
if(rand_string[i] == '!' ||
rand_string[i] == '@' ||
rand_string[i] == '#' ||
rand_string[i] == '$' ||
rand_string[i] == '%' ||
rand_string[i] == '^' ||
rand_string[i] == '&' ||
rand_string[i] == '*' ||
rand_string[i] == '-' ||
rand_string[i] == '+' ||)
coun++;
}
rand_pos = rand()%strlen(rand_string);
for(i=0; i<strlen(rand_string); i++)
{
if(i != rand_pos)
cout<<rand_string[i];
else
cout<<" "<<name<<" ";
}
cout<<"There are "<<count<<" special characters."
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.