Contact Learn C
Copy Program Share Program

Program 250: Program to print Largest and Smallest Word from given Sentence

Program 250: Program to print Largest and Smallest Word from given Sentence

#include<stdio.h>
#include<string.h>
main()
{
    int i=0,j=0,k=0,a,minIndex=0,maxIndex=0,max=0,min=0;
    char str1[100]={0},substr[100][100]={0},c;
    printf("Enter a sentence\n");
    gets(str1);
    while(str1[k]!='\0')//for splitting sentence
    {
        j=0;
        while(str1[k]!=' '&&str1[k]!='\0')
        {
            substr[i][j]=str1[k];
            k++;
            j++;
        }
        substr[i][j]='\0';
        i++;
        if(str1[k]!='\0')
        {
            k++;
        }        
    }
    int len=i;
    max=strlen(substr[0]);
    min=strlen(substr[0]);
    //After splitting getting length of string and finding its index having max length and index having min length
    for(i=0;i<len;i++)
    {
       a=strlen(substr[i]);
       if(a>max)
        {
            max=a;
            maxIndex=i;
        }
        if(a<min)
        {
            min=a;
            minIndex=i;
        }
    }    
  printf("Largest Word is %s \nSmallest word is %s\n",substr[maxIndex],substr[minIndex]);
    
}
Explanation:

//Coming Soon

Output:
Program to print Largest and Smallest Word from given Sentence




5 comments:

  1. while(str1[k]!=' ' && str1[k]!='\0' && str1[k]!='.')
    At line #12, this code changes will help in finding accurate largest and smallest words even if there is a full stop in sentence.

    ReplyDelete

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide