Question

In: Computer Science

Write a function in C++ that reads the line separates it with comma. Input: hello how...

Write a function in C++ that reads the line separates it with comma.

Input:
hello how are you.
hello world
hello_world
I am there for you!

Output:
hello, how, are and you.
hello and world
hello_world
I, am, there, for and you!

just add a comma and (and) before the last word.
CODE IN C++ ONLY.

Solutions

Expert Solution

Here I am writing the code in C++ and after this // i will comment about that line to understand

our logic will be for this program like this.

first we take string in a function then after this function will count how much space are in there in the sentence then after function will see for space if they not getting space so it will print that word if they will get space then count increment and comapre if count == how much counted space intially then print and it will at before last word else print , .

program code:

#include<iostream>    

#include<string.h>      // header file for string

using namespace std;

void add(string str)      // declaring a function with input of str

{

    int j=0,i=0,count=0;    // declare these variable for we used in further function

    for(int k=0;k<=str.size();k++)       // function will run till string length

    {

        if(str[k]==' ')    // it will count for all the space in the string

        {

            j++;

        }

    }

    for(i=0;i<=str.size();i++)    

    {

        if(str[i]==' ')     // checking for spcae in string if come and program enter this loop otherwise it will go to else part

        {

            count++; //first if space found then count incremented

            if(count == j)   // now checking for all the space calculated is equal to space found till now if match then print and it mean cursor in at before the last word

            {

            cout<<" and ";

            }

            else           // else print comma (,)

            {

                cout<<", ";

            }

            

        }

        else

        {

            cout<<str[i];     // if space is not found then print that word

        }

    }

}

int main()      // main function

{

    string s1="hello how are you.";   // declartion of strings

    string s2="hello world";

    string s3="hello_world";

    string s4="i am there for you!";

    add(s1);    // now calling function for string 1

    cout<<endl;   // move the cursor to new line

    add(s2);

    cout<<endl;

    add(s3);

    cout<<endl;

    add(s4);

    return 0;

}

output :


Related Solutions

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”....
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not known to you 3.copies content of input file into an output file named 'output.txt' 4.contents of output.txt should be exactly the same as contents of input.txt 5.the user should be given a choice to select input and output file in any folder 6.remember to check that input file opened successfully
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT