Reverse string model

Reverse string model 

Write a C program to reverse the given string

Sample input   : jobat update
Sample output : update jobat


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
    char str[50];
    gets(str);
    int len;
    for(int i=0;str[i];len++,i++); //length of string
    for(int i=len-1;i>=0;i--)
    {
        if(str[i]==' ' || i==0)
        {
            if(i==0)
                printf("%c",str[0]);
            for(int j=i+1;str[j]!=' '&&str[j]!='\0';j++)
                printf("%c",str[j]);
            printf(" ");
        }
    }
    return 0;
}

OUTPUT:


0 comments