C语言代码问题

2025-02-27 18:28:20
推荐回答(1个)
回答1:

#include 
#include 
#include 

void MaxLenWord(char s[]){
    char *p = s;
    int max=0;
    int currentWordLen;
    int index=0;
    int wordsIndex=0;
    char **words = (char**)malloc(sizeof(char*)*(int)strlen(s)*0.5+0.5);

    while(p!= 0x1){
      index = strcspn(p," ");
      words[wordsIndex] = (char*)malloc(sizeof(char)*index);
      memcpy(words[wordsIndex],p,index);
      words[wordsIndex][index] = 0;
      p = strchr(p,' ')+1;
      currentWordLen = strlen(words[wordsIndex++]);
      if(currentWordLen>max)
          max = currentWordLen;
    }

    for(int i=0;i        if(strlen(words[i]) == max)
            printf("%-*s",max+1,words[i]);
    }
}

int main(){
    int count;
    scanf("%d",&count);

    char **words = (char**)malloc(sizeof(char*)*count);
    for(int i=0;i        setbuf(stdin, NULL);
        words[i] = (char*)malloc(sizeof(char)*100);
        scanf("%[^\n]",words[i],100);
    }

    for(int i=0;i        MaxLenWord(words[i]);
        putchar('\n');
    }

    return 0;
}