Tuesday, May 16, 2017

uva problem solution 483 - Word Scramble



problem lInk

Discussion:  There is tricky part in this problem. Many check string in if condition that not equal to whitespace. and else print the reverse word. but at the the end  word there is problem with the check of whitespace and last word don't print. to avoid it in if condition check whitespace and '\0' character. and  in else store the word in another string.

try your self before see the code.


#include<stdio.h>
#include<string.h>
int main()
{
    char str[1000],str1[1000];
    int i,j,n,cnt;
   while( gets(str))
   {
        cnt=0;

    n=strlen(str);
    for(i=0;i<=n;i++)
    {

if(str[i]==' '||str[i]=='\0')
{
        for(j=cnt-1;j>=0;j--)
        {
            printf("%c",str1[j]);

        }
        cnt=0;
        if(str[i]!='\0')
        printf(" ");
        else if(str[i]=='\0')
        printf("\n");

}
    else {
        str1[cnt]=str[i];
        cnt++;

        }

    }
    }
    return 0;
}

No comments:

Post a Comment