Print the words of a string in reverse

Program:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[]="My name is Allwin";
    int len=strlen(str);
    int i;
    for(i=len-1;i>=0;i--)
    {
        if(str[i]==' ')
        {
            str[i]='\0';
            printf("%s ",&(str[i])+1);
        }
    }
    //To print the first word
    printf("%s",str);
    return 0;
}

Output:

Allwin is name My                                                                                                                             
                    

Comments

Popular posts from this blog

c++ program to print string along the diagonals of the matrix.

C program to print the following Pattern

Kollywood Game