C program to find the longest and smallest word in a string - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Thursday, January 16, 2020

C program to find the longest and smallest word in a string - My CS Tutorial

C program to find the largest and smallest word in a sentence.
In this tutorial we will learn how to write a c program to find or print the largest and smallest word in a sentence.
This is a C program to find largest word in a string.


C program code


#include<stdio.h>
#include<conio.h>
#include<string.h>
void 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;
    clrscr();
    
    printf("\n ENTER A SENTENCE TO FIND LARGEST AND SMALLEST WORD: \n ");
    gets(str1);
    while(str1[k]!='\0')
    {
        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]);
    
    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("\n LARGEST WORD IN THIS SENTENCE IS:\n\n %s \n\n SMALLEST WORD IN THIS SENTENCE IS:\n\n %s\n",substr[maxIndex],substr[minIndex]);

getch();
}


OUTPUT:




In the first we will enter a string or sentence then it will print the longest and smallest word that are available in that string.



Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment