c - 有人可以帮我做这个练习吗
问题描述
我想在不使用指针和数组的情况下获得正确的结果。
这是代码(变量 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;
}
解决方案
这是要查找的代码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;
}
过程是:
- 一次取出整个字符串
- 遍历字符串并找到最长的单词长度
虽然我不懂法语。但是,我希望我走的是正确的道路。您应该能够自己找出其他解决方案。
推荐阅读
- c# - 实体框架导航属性在分配 Id 后为空
- dependencies - Ubuntu 18.04 libosmesa6 依赖 libglapi-mesa (= 18.0.0~rc5-1ubuntu1) 但要安装 18.0.5-0ubuntu0~18.04.1
- javascript - 如何更改滑块上的动态属性
- css - 如何在 Angular 6 项目中使用 less & bootstrap?
- r - R核心的`split`函数背后的算法是什么?
- crystal-reports - 如何在 Crystal Report 预览/打印(详细信息部分)上显示 TextObject?
- angular - Angular可以激活不触发
- python - keras extract best val loss
- java - Check Leap Year - without using division operation
- mysql - composite index is not work in range query