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.
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
(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.
Question 1: Write a C program that reads a date from the keyboard and tests whether...
Question 1: Write a C program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid month...
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
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”....
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT