Words with Length N ID:6908

The program must accept a  space separated string and an integer as the input. The program must print the words from S which are of length N as the output. If no word is of length N then the program must print -1 as the output.

Boundary Condition(s):
1 <= Length of S <= 1000

Example Input/Output 1:
Input:
Have a good day
4

Output:
Have good

Example Input/Output 2:
Input:
help the need
2

Output:
-1

PROGRAM IN C:


#include<stdio.h>

#include <string.h>

int main()

{char s[10001][10001];int i=0;

int k[1000],f=0;

while(scanf("%s",s[i])>0)

{k[i]=strlen(s[i]);

i++;

}

int d=(int)s[i-1][0]-48;

for(int j=0;j<i;j++)

{

    if(k[j]==d)

    {printf("%s ",s[j]);

    f=1;

    }

}

if(f==0)

printf("-1");

}


Comments

Popular Posts