用c语言定义一个函数func()完成字符串长度的计算,在func()函数中,通过while循环遍历字符串,遍历时使用指针遍历,从键盘输入字符串。
时间: 2023-05-19 12:03:27 浏览: 70
可以使用以下代码来定义一个函数func(),完成字符串长度的计算:
```c
#include <stdio.h>
int func() {
char str[100];
int len = 0;
char *ptr = str;
printf("请输入一个字符串:");
scanf("%s", str);
while (*ptr != '\0') {
len++;
ptr++;
}
return len;
}
int main() {
int len = func();
printf("字符串的长度为:%d\n", len);
return 0;
}
```
在这个函数中,我们首先定义了一个字符数组str和一个整型变量len,用于存储输入的字符串和计算字符串长度。然后定义了一个指针ptr,指向字符数组的首地址。
接下来通过scanf()函数从键盘输入字符串,并使用while循环遍历字符串,每遍历一个字符,就将len加1,最后返回计算出的字符串长度。
在主函数中,我们调用了func()函数,并将返回的字符串长度打印出来。
相关问题
用c语言定义函数func()实现两个字符串大小的比较 l 在func()函数中,通过while循环完成两个字符串的比较,从键盘输入两个字符串。
以下是用 C 语言定义函数 func() 实现两个字符串大小比较的代码:
```c
#include <stdio.h>
#include <string.h>
int func(char str1[], char str2[]) {
int i = 0;
while (str1[i] == str2[i]) {
if (str1[i] == '\0' || str2[i] == '\0')
break;
i++;
}
if (str1[i] == '\0' && str2[i] == '\0')
return 0;
else if (str1[i] < str2[i])
return -1;
else
return 1;
}
int main() {
char str1[100], str2[100];
printf("请输入第一个字符串:");
scanf("%s", str1);
printf("请输入第二个字符串:");
scanf("%s", str2);
int result = func(str1, str2);
if (result == 0)
printf("两个字符串相等\n");
else if (result < 0)
printf("第一个字符串小于第二个字符串\n");
else
printf("第一个字符串大于第二个字符串\n");
return 0;
}
```
输入两个字符串后,程序会调用 func() 函数进行比较,返回值为 0 表示两个字符串相等,返回值为 -1 表示第一个字符串小于第二个字符串,返回值为 1 表示第一个字符串大于第二个字符串。
请在Linux利用c语言编程实现主次线程,当主线程从主键盘输入一个字符串后,从线程能打印出这个字符串的长度,并且当字符串为“exit”时退出次线程
下面是一份实现主次线程的示例代码:
```
#include <pthread.h>
#include <stdio.h>
#include <string.h>
void *thread_func(void *arg)
{
char *str = (char *)arg;
int len = strlen(str);
printf("字符串长度为:%d\n", len);
pthread_exit(NULL);
}
int main()
{
pthread_t thread;
char str[100];
while (1) {
printf("请输入一个字符串:");
scanf("%s", str);
if (strcmp(str, "exit") == 0) {
break;
}
int ret = pthread_create(&thread, NULL, thread_func, (void *)str);
if (ret != 0) {
printf("创建线程失败\n");
return -1;
}
pthread_join(thread, NULL);
}
return 0;
}
```
这段代码利用 `pthread_create` 函数创建一个新线程,执行 `thread_func` 函数。主线程在循环中等待用户输入字符串,每输入一个字符串就创建一个新线程来打印它的长度。如果字符串为 `"exit"`,则退出循环。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)