Q:

C program to print the biggest word in a string

belongs to collection: C String Programs

0

C program to print the biggest word in a string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Given a string, we have to print the biggest word from the given string using C program.

Program:

The source code to print the biggest word in a string is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to print the biggest word in a string

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    int i = 0;
    int j = 0;
    int flag = 0;

    char str[64];
    char word[64];

    char biggest[16];

    printf("Enter a string: ");
    fflush(stdin);
    scanf("%[^\n]s", str);

    for (i = 0; i < strlen(str); i++) {
        while (i < strlen(str) && !isspace(str[i]) && isalnum(str[i])) {
            word[j++] = str[i++];
        }
        if (j != 0) {
            word[j] = '\0';
            if (!flag) {
                flag = !flag;
                strcpy(biggest, word);
            }
            if (strlen(word) > strlen(biggest)) {
                strcpy(biggest, word);
            }
            j = 0;
        }
    }

    printf("Biggest Word in string is: %s\n", biggest);

    return 0;
}

Output:

RUN 1:
Enter a string: India is a great country
Biggest Word in string is: country

RUN 2:
Enter a string: is are am
Biggest Word in string is: are

RUN 3:
Enter a string: ram mohan mohandas ramdas
Biggest Word in string is: mohandas

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to reverse a string using recursion... >>
<< C program to print the smallest word in a string...