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 a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a...
Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a checkerboard of 3-by-3 squares. (It should work even if the input dimensions are not a multiple of three.) . Don't use function. Example 1: Input width: 16 Input height: 11 Shape: *** *** *** *** *** *** *** *** *** *** *** * *** *** * *** *** * *** *** *** *** *** *** *** *** *** *** *** * *** *** *...
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...
User asks you to develop a program that calculates and then prints interest earned on a...
User asks you to develop a program that calculates and then prints interest earned on a bank balance. Program should print interest earned for the same balance when interest is accumulated annually, semiannually and quarterly. I need the C++ code!
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
Q9) Write a function that asks the user for a number and prints out the multiplication...
Q9) Write a function that asks the user for a number and prints out the multiplication table for that number. Python3 Q10)Write a function that takes two integer inputs for width and length, and prints out a rectangle of stars. (Use * to represent a star). Q11)Write a program that reads in a string and prints whether it: [Hint: given a character like ‘H’ you can apply to it the method ‘H’.isalpha() to check if it is a letter or...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT