首页 > 解决方案 > 有人可以帮我做这个练习吗

问题描述

我想在不使用指针和数组的情况下获得正确的结果。

这是代码(变量 j,k 删除无用):

#include <stdio.h>

int main(void) {
    char c;
    int i;

    for (i = 0; c != '#'; i++) {
        c = getchar();
        printf("%c", c);
    }

    i -= 1;
    printf("le nombre de charactere dans cet phrase est :\t %d \n", i);
    
    return 0;
}

标签: c

解决方案


这是要查找的代码The length of the longest word in the sentence

#include <stdio.h>

int main(void) {
    int longest_word_len = 0, temp_len = 0;
    char text[100];
    scanf(" %[^\n]s", text);
    for (int i = 0;  text[i]!= '#'; i++) {
        if(text[i] == ' '){
            if(temp_len > longest_word_len){
                longest_word_len = temp_len;
            }
            temp_len = 0;
        }else{
            temp_len++;
        }
    }
    
    printf("the length of the longest word in the sentence %d", longest_word_len);

    return 0;
}

过程是:

  1. 一次取出整个字符串
  2. 遍历字符串并找到最长的单词长度

虽然我不懂法语。但是,我希望我走的是正确的道路。您应该能够自己找出其他解决方案。


推荐阅读