First word of sentence (by individual character printing) – CSKC
Sign In

Search

Solved Problem #CPP#2594    siteicon   siteicon  

Problem Statement - First word of sentence (by individual character printing)

Write a program to print the first word of a sentence given by the user.

Solution

TC++ #2594

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Alternate (?) :

char str[80]; cout<<“Input a small sentence:” ; cin.getline(str,80);
User given sentence is collected in str.

for(int i=0;str[i]!=’ ‘;i++) cout<<str[i];
The loop runs from first index position to the place where first space in the sentence occurs. In each iteration it keeps printing the character it keeps finding, so finally we are able to see the word on screen as the loop finishes.

Notes

  • In this code we are not separating the word to a different string. We can do so easily by using the another char array and copying character by character to the second array.


Suggested Filename(s): firstwrd.cpp



Share

Created: 13-Jan-2018 17:40 | Updated: 13-Jan-2018 18:25|