Computer Science‎ > ‎

C Program: Reversing the order of words in a sentence




 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

       
Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

                               
 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart

             
The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
            Buy from FlipKart


          
C How to Program (Deitel and Deitel)

 




C Program: Source code to reverse the order of words in a sentence


/* This is a program in C to reverse the order of words in a line of text.
*/
#include<stdio.h>
#include<string.h>
int main()
{
char *text=(char *)malloc(100*sizeof(char));//the array to store the entered sentence
char *temp=(char *)malloc(10*sizeof(char));//used for storing words
printf("Enter the line of text(length<100 and word length <10)\n");
gets(text);//use gets
int ctr=0;
int words=1;//initalize words as one because there would be atleast one word
int row;
while(text[ctr]!='\0')
if(text[ctr++]==' ')
words++;//count number of words by counting spaces
char **word=(char **)malloc(words*sizeof(char));//A 2-D array of words is made
for(row=0;row<words;row++)
word[row]=(char *)malloc(10*sizeof(char));
int len=0;
row=0;
while(len<strlen(text))
{
sscanf(text+len,"%s",temp);//scanf from appropriate length
strcpy(word[row++],temp);//copy the extracted word to the 2D array
len=len+strlen(temp)+1;//scan the next word so sum up the length for previous word and 1 for the space
}
char *swaptemp=(char *)malloc(10*sizeof(char));
for(row=0;row<words/2;row++)
{
strcpy(swaptemp,word[row]);//swap the first with last second with second last and so on
strcpy(word[row],word[words-row-1]);
strcpy(word[words-row-1],swaptemp);
}
strcpy(text,"");
for(row=0;row<words;row++)
{
     strcat(text,word[row]);
    strcat(text," ");//form the new text by concatenating the reverse words
}
printf("%s",text);
getch();
}
/*A sample run of the program works as:-
Enter the line of text(length<100 and word length <10)
The best place to learn is learning point
point learning is learn to place best The
*/




 










C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers

Try out our Quizzes!