C Program to reverse the sentence by words

Program to reverse the sentence by words

This program is used to reverse the sentence which is passed to the main function using command line arguments.

Example:

we are taking the input from the user using command line arguments.
input = $ ./a.out Subhash loves India
result = India loves Subhash

Logic:

1.Take the input from the user.
2.Use malloc and allocate memory for the given input.
3.Now the pointer(p) created will be pointing to the allocated memory.
4.Copy the contents of the input given using command line argument into the memory allocated ( *p = argv[i][j]) and input character ‘ ‘ and ‘\0’ in appropriate position.
5.Call the function my_strrev(p) by passimg the pointer p.
6.In the function defination ,consider two variables i and j, such that i points to the first character and j to the last character, perform the swapping function until (i<j)is satisfied.
7.When (i < j) condition fails the condition comes out of the loop and prints the required reverse string.
8.Call the function my_strword(p) 3 (i.e. argc – 1) times to again reverse the individual words of the string.

 

Source code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void my_strword(char *);
void my_strrev(char *);
int main(int argc,char *argv[])
{
            int i,j,n=0,sum = 0;
           for(i=1;i<argc;i++)
           {
                         sum = sum + strlen(argv[i]);
           }
           sum = sum + argc-1;
           char *p = malloc(sum);
           char *q = malloc(sum);
           q = p;
          for(i = 1;i<argc;i++)
          {
                      for(j=0;argv[i][j]!='\0';j++)
                     {
                                *p=argv[i][j];
                                  p++;
                     }
                   *p++ =' ';
         }
         *--p = '\0';
          p = q;
         printf("\n\ninput = %s\n",p);
         my_strrev(p);
         for(i=1;i<argc;i++)
        {
                    my_strword(p);
        }
        printf("word reverse = %s\n\n\n",p);
}
void my_strrev(char *p)
{
           int i,j;
          for(i=0,j=strlen(p)-1; i<j; i++,j--)
         {
                    char t;
                    t = *(p+i);
                    *(p+i) = *(p+j);
                    *(p+j) = t;
          }
          printf("reversed string = %s\n",p);
}
void my_strword(char *p)
{
          static int i,a=0;
          int k,j;
          while(*(p+i) !=' ')
          {
                      if(*(p+i) == '\0')
                    {
                                  break;
                    }
                    i++;
         }
         --i;
           for(k=a,j=i; k <j; k++,j--)
          {
                       char t;
                       t = *(p+k);
                        *(p+k)=*(p+j);
                        *(p+j)=t;
          }
           a = i = (i+2);
}

 

This article is written by one of my student as part of their blog writing exercise.

To learn more programs on C and C++, click this link: Interview Questions

Subhash.K.U, Principal Mentor, Subhash Embedded Classes,Bangalore.

VIEW SUBHASH’s PROFILE

SUBHASH LOVES TO TEACH ! CLICK HERE !

Subhash Embedded Classes is one of the Best Embedded Systems Training Institute in Bangalore

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.