1. Write a C++ program to perform the following tasks. Define an array of charac
ID: 3882838 • Letter: 1
Question
1. Write a C++ program to perform the following tasks. Define an array of characters of size 15. Read 15 characters from the user into the array. Next, read a search character from the user. The program should search for this character in the array. If found, report so, and also report the number times the search character occurs in the input array. Finally, the program should report what other character(s) in the input array occur(s) the same number of times as the search character. Suppose that your 15 character input is “aaaabbbbccccdef”, and the search character is ‘a’. Your program would state that ‘a’ is present in the input and that it occurs 4 times. It would also output stating that “The characters ‘b’ and ‘c’ occur the same number of times as that of ‘a’”.
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
int main() {
char text[15], search;
cout<<"Enter the text: ";
cin>>text;
cout<<" Enter the search character: ";
cin>>search;
int count = 0;
for(int i=0; text[i] != ''; i++)
{
if(text[i] == search)
{
count++;
}
}
if(count == 0)
cout<<endl<<search<<" was not found."<<endl;
else
{
cout<<endl<<search <<" was found "<<count<<" times"<<endl;
}
char moreSearches[15];
int size = 0;
for(int i=0; text[i] != ''; i++)
{
if(text[i] == search || text[i] == '-')
continue;
int subCount = 0;
for(int j=0; text[j] != ''; j++)
{
if(text[i] == text[j])
{
text[j] = '-';
subCount++;
}
}
if(subCount == count)
moreSearches[size++] = text[i];
}
if(size > 0)
{
cout<<"The characters ";
for(int i=0; i<size; i++)
{
cout<<moreSearches[i]<<", ";
}
cout<<" occur the same number of times as "<<search;
}
else cout<<"No other character appears the same number of times as "<<search<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.