Question

In: Computer Science

Using c++, write a program that reads a sequence of characters from the keyboard (one at...

Using c++, write a program that reads a sequence of characters from the keyboard (one at a
time) and creates a string including the distinct characters entered and displays the
string on the screen. The input terminates once the user enters a white-space
character or the user has entered 50 distinct characters.

Do not use C-Strings.
2. Use the following function to append character “ch” to the string “s”:
s.push_back(ch);
3. Read the input characters one by one, i.e. do not read the input as a string.
4. Do not use arrays.

Solutions

Expert Solution

The required C++ code is as follows:----

#include<bits/stdc++.h>
using namespace std;
int main(void)
{
        string s; //  String to store the characters 
        s="";
        
        char ch;
        
        
        while(s.length()!=50) // Checking whether the length of string is 50 or not
        {                                     
                cout<<"Enter a character"<<endl; // Prompt the user to enter the next character
                cin>>ch;
                
                if(isspace(ch)) // isspace (char ch) is a function in C which checks if the character passed as an argument is a whitespace character or not
                break; // Break out of loop if character entered is a whitespace character...
                
                bool found=false; // variable to check if the recently entered character is a distinct character or not
                for (int i=0;i<s.length();i++) // Loop to check if the recently entered character is already present in the string or not
                {
                        if(s[i]==ch)
                        found=true;
                }
                 
                if(!found) // If the character is distinct we append it to the string...automatically length of string increases by 1..
                s.push_back(ch); // We push the recently entered distinct character at the end of the string..
                
        }
        
        if(s.length())
        cout<<"The resultant string is --- "<<s<<endl;
        else
        cout<<"The resultant string is an empty string"<<endl; // Case when the first character entered by the user is a whitespace character
}

Related Solutions

Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
Write a C++ function called parse that reads one line of user input from the keyboard...
Write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input.  Your function should be passed the array and a reference variable that is to be assigned the length of the array.  Prompt the user and read the input from within the function. For example:  If the user inputs copy this that, the resulting array would have length 3 and contain the strings “copy”, “this”, and “that”....
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a program that reads three values from the keyboard representing, respectively, an investors name, an...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an investment amount, and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate) . Your program should calculate and output (in currency notation) the future value of the investment in 10, 2 20 an 30 years using the following formula:   Future value =investment(1+interest rate)year Example of a test run: Enter...
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT