Question

In: Computer Science

how to code the text with excess whitespace removed in C++. Whitespace includes spaces and tabs....

how to code the text with excess whitespace removed in C++. Whitespace includes spaces and tabs. Excess whitespace is:

• All whitespace at the beginning of the text.

• All whitespace at the end of the text.

• Any point in the text where more than a single whitespace character is used.

For instance, if the input text is: Welcome to C++ !

The output would be: Welcome to C++!

Solutions

Expert Solution

Sample output:

The screenshots are attached below for reference.

Please follow them for proper indentation and output.

Program to copy:

#include <iostream>
#include<string>
using namespace std;

int main()
{
char str[100]={0};
  
cout<<"Enter text:";
cin.getline(str,100);//read input from user
int i=0;
int k=0;
int space=0;
while(str[i]!='\0'){//loop till end of input
while(k==0 && str[i]==' ')//removinf spaces if oresent in beginning
i++;
  
if(str[i]==' ')//if space occurs check for previous character
{
if(space==0){
str[k]=str[i];
k++;
space=1;
}
}
  
else if(str[i]=='!'){//check punctuation caracters
if(str[k-1]==' '){
str[k-1]=str[i];
}
else{
str[k]=str[i];
k++;
}
space=0;
}
else{
str[k]=str[i];
k++;
space=0;
}
i+=1;
}
str[k]='\0';//end the string
i=0;
while(str[i]!='\0'){//print the result string
cout<<str[i];
i++;
}
return 0;
}


Related Solutions

3. How are the excess starting materials removed from the alum product? 4. What is the...
3. How are the excess starting materials removed from the alum product? 4. What is the purpose of using isopropyl alcohol in this experiment? 5. During the washing step, how can you tell when your product has been sufficiently purified?
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Write a c or matlab text code(to be copied ) for Huffman coder and Huffman decoder...
Write a c or matlab text code(to be copied ) for Huffman coder and Huffman decoder that asks the user to enter the string and output the Huffman code for every letter and a code for encoding that will have every letter and its Huffman code and output all the possibilities for the real string. you must show a screen of an input and the output for both the encoder and the decoder
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
TEXT ONLY PLEASE ( PLEASE NO PDF OR WRITING) C++ CODE Instructions In a right triangle,...
TEXT ONLY PLEASE ( PLEASE NO PDF OR WRITING) C++ CODE Instructions In a right triangle, the square of the length of one side is equal to the sum of the squares of the lengths of the other two sides. Write a program that prompts the user to enter the lengths of three sides of a triangle and then outputs a message indicating whether the triangle is a right triangle. If the triangle is a right triangle, output It is...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Summary Samantha and Vikas are looking...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Summary Samantha and Vikas are looking to buy a house in a new development. After looking at various models, the three models they like are colonial, split-entry, and single-story. The builder gave them the base price and the finished area in square feet of the three models. They want to know the model(s) with the least price per square foot. Instructions Write a program that accepts as input the base...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT