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

-1

I came across this problem during a personal project, it is even more curious, I wanted to please ask anyone who can give me a light with this, since I have solved the problem in other languages only in C that have not yet. p>

How can I do to detect the largest word in a string, in the case of a phrase typed by the user? I'm having difficulty right in the part when I detect the first whitespace, inside the 'If' inside the 'for' 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 not continue, I do not know how to keep comparing, and he always gives me the last word of the sentence, never the greater. I have tried to set up my own functions when I detect the first space, it validates the string up to that point and copies it to another char string, but it did not work.

If someone has a solution, I could only solve this with the function in JAVA that calls Split that fragments the string into a vector with each index being a word, it is easier to compare. I can not imagine if something similar can be done in C. More to heal this curiosity of mine that I come here. Thanks for the time, thank you and hug!

    
asked by anonymous 09.05.2017 / 04:54

2 answers

0

The idea of this function below is to store in a temporary where the word begins and what its length. Then it checks if the temporary is greater than the largest word found so far and if so, it becomes the new biggest 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;
}
    
09.05.2017 / 05:46
0

The function below takes one string per parameter and returns the largest word contained in this String. She checks the size of the biggest word and puts it in 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, "
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, "%pre%");
    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;
}
"); 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; }
    
09.05.2017 / 06:26