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”....
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
Write a program that reads a line of text input by the user and places each...
Write a program that reads a line of text input by the user and places each word in a TreeSet. Print the elements of the TreeSet to the screen. This will cause the elements to be printed in ascending order. Using Eclipse for this. TreeSetUse.java.
(Please write in C++) Write a program that reads in a line consisting of a student’s...
(Please write in C++) Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [ ]...
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...
C++ Write a program that reads in a list of 10 names as input from a...
C++ Write a program that reads in a list of 10 names as input from a user and places them in an array. The program will prompt for a name and return the number of times that name was entered in the list. The program should output total number of instances of that name and then prompt for another name until the word done is typed in. For this lab, use the string data type as opposed to char to...
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...
For example, I have an input text file that reads: "Hello, how are you?" "You look...
For example, I have an input text file that reads: "Hello, how are you?" "You look good today." "Today is a great day." How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.) This is the linked list code: #include <iostream> #include <cstdlib> #include <fstream> class Node { public: Node* next; int data;...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT