Search Here

Tuesday, June 30, 2015

UVa - 12543 ( Longest Word Solution )

Try Yourself First. For help Scroll Down .

Clearance : To solve this problem you have to take an string as an input until " E-N-D " comes . then for each word if characters of that word is an alpha bate (a-z / A-Z ) or an hyphen ( - )  then count the word length and copy the word if its length is greater than the previously greater length's word . And now print it .

Code


#include<bits/stdc++.h>

using namespace std;

int main()
{
    char s[150];
    char r[150]="";
    int maxl = 0;
    while(scanf("%s",s))
    {
        if(strcmp(s,"E-N-D")==0)  break;
        int len = strlen(s);
        int valid = 0;
        for(int i=0; i<len; i++)
        {
            if(isalpha(s[i])||s[i]=='-')
            {
                valid++;
            }
        }
        if(valid>maxl)
        {
            maxl = valid;
            strcpy(r,s);
        }
    }
    int l = strlen(r);
    for(int i=0; i<l; i++)
    {
        if(isalpha(r[i]) || r[i]=='-')
             { printf("%c",tolower(r[i])); }
    }
    printf("\n");
    return 0;
}


=> Question ?? Leave a Comment .

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Code like this gets WA :/ i wrote similar one and idk what is wrong :/

    ReplyDelete
    Replies
    1. Post your Code here using ideone.com

      Delete