Reversing each word in a sentence – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Solved Problem #CPP#3073 siteicon   siteicon   siteicon  

Problem Statement - Reversing each word in a sentence

Write a program to take a sentence input from the user and then print the sentence by reversing each word in the sentence.

Solution

TC++ #3073

 

Run Output

Input Any Sentence : Human knowledge is still just a drop in the ocean
namuH egdelwonk si llits tsuj a pord ni eht naeco

char s[80]; cout<<“Input Any Sentence : ” ; cin.getline(s,50);
Here we collect the sentence from the user.

for(int i=0;s[i]!=’\0′;i++)
This is outer loop to traverse the whole string till null character is found.
{
if(s[i+1]==’ ‘ || s[i+1]==’\0’)
This condition tests the word boundary if the next character is a space or the null character (for end of sentence)
{
for(int j=i; j>=0 && s[j]!=’ ‘; j–)
If the next character is after the word boundary then current index is definitely the last character of the word. In such a case we begin from the last character of the word to first character of the word by checking the backward word boundary, which can be space again or is up to the first character.

cout<<s[j];
We keep outputting till the word boundary is arrived in the reversed form.

cout<<” “;
This is to manually give a space if the reversed word is printed. This is done after the inner loop but within the outer loop.
}
}

 

Notes

  • In this method if any punctuation marks are there in the sentence, then they will also form the part of reversed word. User’s are encouraged to modify this program to check for any punctuation marks instead of checking just the space and last character for word boundaries.


Share

sunmitra| Created: 27-Jan-2018 | Updated: 15-Jan-2019|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back