C Program To Find The Largest Word Of String | Coderbyte

C Program To Find The Largest Word Of String | Coderbyte

Largest Word Of String

Write a program to find the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and other special characters.

Input Format:

We need to enter the sentence which we want to find the largest word of the sentence.

Output Format:

Output will be shown the largest word in the given input sentence.Here, word considered as a form of alpha numeric characters(a-Z, 0-9). Special characters are not consider as word.

Link: http://www.coderbyte.com

Sample Test Cases

Logic

  • In the main function get the string from the user using scanf function.
  • Call the function named largestWord() take the sen parameter being passed .
  • Traverse through the string using for loop until strlen(sen).
  • Check the each characters of the string to ASCII value of alpha numeric charaters then store into buff and compare if length of  buff is greater than length of word then copy buff into word.
  • If special characters occurs then initialize buff into empty.
  • After completing the loop then print word which we want to find the largest word of the sentence.

 

Program

#include <stdio.h>
#include <string.h>
void largestWord(char *sen) {
  char word[200]="";
  char buff[200]="";
  int n=0,i;
  for(i=0;i<strlen(sen);i++)
  {
      if((sen[i]>=97 && sen[i]<=122)||(sen[i]>=65 && sen[i]<=90)||(sen[i]>=48 && sen[i]<=57))
        buff[n++]=sen[i];
      else
	    n=0;// Reset for next word
      if(strlen(buff)>strlen(word))
        strcpy(word,buff);
  }
  printf("%s",word);
}
int main(void) {
    char sen[200];
    scanf("%[^\n]s",sen);
    largestWord(sen);
    return 0;
}

You Might Also Like…

To print all palindromes present in a word

Conversion program from number to words

 


Hari Prasath R

You know who I am :)