用C语言编写程序,输入若干个字符串,求出每个字符串的长度,并打印最长一个字符串的 内容。以 “stop” 作为输入的最后一个字符串。
时间: 2024-05-03 18:22:22 浏览: 23
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, max_len = 0;
char max_str[100];
while (1) {
printf("请输入一个字符串:");
scanf("%s", str);
if (strcmp(str, "stop") == 0) {
break;
}
len = strlen(str);
printf("该字符串的长度为:%d\n", len);
if (len > max_len) {
max_len = len;
strcpy(max_str, str);
}
}
printf("最长的字符串为:%s\n", max_str);
return 0;
}
```
注意:本程序假设每个输入的字符串长度不超过99个字符。如果需要处理更长的字符串,可以将数组长度适当增大。
相关问题
c语言 编写程序求一个字符串的长度。 输入形式: chinese! 输出形式: 8
以下是一个求字符串长度的 C 代码示例:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
printf("该字符串的长度为:%d\n", len);
return 0;
}
```
在这个示例程序中,我们使用了 `strlen()` 函数来求字符串的长度。这个函数需要包含头文件 `string.h`。我们先定义了一个字符数组 `str` 来存储输入的字符串,再定义一个整型变量 `len` 来存储字符串的长度。接着,我们使用 `scanf()` 函数从标准输入中读入一个字符串,并用 `strlen()` 函数计算其长度。最后,我们使用 `printf()` 函数输出字符串的长度。
用c语言编写程序实现输入一个字符串,求其长度。(不用strlen函数实现)
可以使用循环来实现字符串长度的计算,具体实现如下:
```c
#include <stdio.h>
int main() {
char str[100];
int len = 0;
printf("请输入一个字符串:");
gets(str); // 读入字符串
while (str[len] != '\0') { // 循环计算字符串长度
len++;
}
printf("该字符串的长度为:%d\n", len);
return 0;
}
```
上述程序中,我们声明了一个字符数组 `str` 和一个整型变量 `len`,并使用 `gets()` 函数读入字符串。然后,我们使用 `while` 循环遍历字符串中的每个字符,直到遇到字符串结束标志 `\0` 为止,每遍历一个字符,`len` 变量就加 1。最后输出 `len` 即可得到字符串的长度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)