How to detect the largest word of a string in C?

Asked

Viewed 2,149 times

-1

I came across this problem during a personal project, it is more curiosity really, I would like to ask anyone who can give me a light on this, because I have solved the problem in other languages only in C that not yet.

How can I detect the largest word of a string in the case of a phrase typed by the user? I’m having trouble with the part when I detect the first white space, inside the 'If' loop' .

for(i = 0; str[i] < MAX; i++){
        if(str[i] == ' '){ //CONTINUAR a string depois do primeiro espaço;
            strcpy(maior, str); //Copia palavra em maior
            strcpy(temp, str);
        }
  • Right here, I can’t go on, I don’t know how to keep comparing and he always gives me back the last word of the sentence, never the greatest. I have tried to assemble proper functions for when detecting the first space, it validates the string up to that point and copies to another char string, but came to nothing.

If someone has a solution, I was only able to solve this with the JAVA function that calls Split that fragments the string into an array with each Input being a word, then it is easier to compare. I can’t imagine if something like this can be done in C. More to heal this curiosity of mine that I come here. Thank you for taking the time, thank you and hug!

2 answers

0

The function below takes a string per parameter and returns the largest word contained in that String. It checks the size of the largest word and puts it into a temporary one returning at the end the biggest word.

char* maior_string(char *s){
    int temp = 0, fim = 0; 
    char *maior = (char *) malloc(sizeof(char) * strlen(s));
    if(!maior)
        exit(EXIT_FAILURE);
    strcpy(maior, "\0");
    while(*s){
        if(*s == ' '){
            if(fim > temp){
                strncpy(maior, s - fim, fim);
                temp = fim;
            }
            fim = 0;
        } else 
            fim++;
        s++;
    }
    if(fim > temp){
        strncpy(maior, s - fim, fim);
        temp = fim;
    }
    return maior;
}

0

The idea of this function below is to store in a temporary where the word begins and how long it is. Then it checks if the temporary is larger than the largest word found so far and if it is, this becomes the new larger word.

#include<stdio.h>

int main(){
    char palavra[200];
    printf("Insira uma palavra: ");
    //scanf("%s", palavra);
    strcpy(palavra, "Como eu detectar a maior palavra de uma string");
    int tamanho = strlen(palavra);

    int maior=0, ini=0; //variaveis da maior palavra
    int cont=0, temp_ini=0; //variaveis do temporario
    int i;
    for (i = 0; i <=tamanho; i++){
        //se a frase terminou ou a palavra terminou
        if(i==tamanho || palavra[i] == ' '){
            if(maior<cont){
                maior=cont;
                ini=temp_ini;
            }
            cont=0;
            temp_ini=i+1;
        }
        else{
            cont++;
        }
    }

    printf("\nMaior palavra: ", ini);
    for (i = ini; i <ini+maior; i++)
        printf("%c", palavra[i]);
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.