编写C语言程序,定义一个2行40列的字符型二维数组,以字符串格式从键盘中输入两个长度不大于39的字符串到二位数组各行,求两个字符串的字符个数。
时间: 2024-05-11 11:15:54 浏览: 93
您好,我明白您的问题。以下是解答:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[40];
char str2[40];
char arr[2][40];
printf("Please enter two strings that are no longer than 39 characters each: \n");
fgets(str1, sizeof(str1), stdin);
fgets(str2, sizeof(str2), stdin);
// Remove newline characters from input
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
// Copy the strings into the 2D array
strcpy(arr[0], str1);
strcpy(arr[1], str2);
// Output the number of characters in each string
printf("The first string has %d characters.\n", strlen(arr[0]));
printf("The second string has %d characters.\n", strlen(arr[1]));
return 0;
}
```
这段代码会要求用户输入两个字符串,然后将它们复制到一个2行40列的字符型二维数组中,并分别计算每个字符串的字符数。
阅读全文
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)
![](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)