Question

In: Computer Science

Write a short program that asks the user for a sentence and prints the result of...

Write a short program that asks the user for a sentence and prints the result of removing all of the spaces. Do NOT use a built-in method to remove the spaces. You should programmatically loop through the sentence (one letter at a time) to remove the spaces.

Solutions

Expert Solution

C++ program:

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
   cout<<"Enter a String of your choice"<<endl;
string s;
getline(cin, s);
int n = s.length(); // find length of string
  
/*convert String into String array*/
  
char string_array[n + 1];// declare String array
s.copy(string_array, s.size() + 1); // copy string into String array
string_array[s.size()] = '\0'; // enter \0 at the end of string array
  
   /*Removing Spaces*/
  
int i = 0, j = 0;
while (string_array[i]) // while loop till end of the string
{
if (string_array[i] != ' ') { // condition to remove string
                           // if no space then put it into array otherwise left it
                           // if there is space then condition is fail and increment i
                           // and check for another charector
string_array[j++] = string_array[i];
       }
i++;
}
string_array[j] = '\0'; // add '\0' at the end of string
cout<<"String with no whitespace :"<<string_array<<endl; // print String

return 0;
}

Output 1:

Output 2:

Program Snapshot

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
        cout<<"Enter a String of your choice"<<endl;
    string s;
    getline(cin, s); 
    int n = s.length();  // find length of string
    
    /*convert String into String array*/
    
    char string_array[n + 1];// declare String array
    s.copy(string_array, s.size() + 1); // copy string into String array
    string_array[s.size()] = '\0'; // enter \0 at the end of string array 
    
        /*Removing Spaces*/
        
    int i = 0, j = 0; 
    while (string_array[i])  // while loop till end of the string
    { 
        if (string_array[i] != ' ') {  // condition to remove string
                                                                // if no space then put it into array otherwise left it
                                                                // if there is space then condition is fail and increment i
                                                                // and check for another charector
           string_array[j++] = string_array[i];
                }
        i++; 
    } 
    string_array[j] = '\0';  // add '\0' at the end of string
   cout<<"String with no whitespace :"<<string_array<<endl; // print String
   
   return 0;
}

Related Solutions

Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write an interactive C program that asks a user to enter a sentence from the keyboard,...
Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards. Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space. Sample code execution: bold information entered by user enter a sentence: hello ece 175 class olleh ece 571 ssalc continue (q to quit)? y enter a sentence: May the force be with you...
Write a class to accept a sentence from the user and prints a new word in...
Write a class to accept a sentence from the user and prints a new word in a terminal formed out of the third letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “neltd”. Program Style : Basic Java Programming
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT