Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Loops IO Using C++ Programming, code a program that can do the following 1. Read

ID: 3878666 • Letter: L

Question

Loops IO

Using C++ Programming, code a program that can do the following

1. Read a sentence from the console.

2. Break the sentence into words

3. Iterate over each word, if the word is a numeric value then print its value doubled, otherwise print out the word, with each output on its own line.

*Cannot use vector or cytpe*

*shouldn't be a lengthy program*

*Can only use :

<iostream>
<stdio.h>
<stdlib.h>
using namespace std;

Sample Run:

Sentence: Hello world, there are 3.5 items.

Output:

Hello

world,

there

are

7

items.

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

int main()
{
    string str;
    while(cin>>str)                           //Takes in input word by word and stops when the sentence is over
    {
        int check=0;                          //Initialises check variable to 0
        double number;
      
        for(int i=0;i<str.length();i++)
        {
            if(str[i]<'0' || str[i]>'9')      //This condition is true whenever the character is not a digit
            {
                if(str[i]=='.')       
                {
                    check++;                  //When we get only 1 decimal point, it is a number.
                }                             //Any string with 2 or more decimal points is not a number
                else
                {
                    check+=2;                  // If we get any other character, we add 2
                }
            }
        }
        if(check>=2)                            //This ensures that the string is not of number type
            cout<<str<<endl;                    //And we print the string
        else
        {
            number = atof(str.c_str());         // This function is in stdlib and converts string to float
            cout<<2*number<<endl;
        }
    }
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote