手动实现strlen
时间: 2023-12-19 20:05:40 浏览: 31
手动实现strlen函数的方法如下:
1. 定义一个计数器变量count,初始化为0。
2. 从字符串的第一个字符开始遍历,每遍历一个字符,计数器count加1。
3. 当遍历到字符串的结束字符'\0'时,停止遍历。
4. 返回计数器count的值,即为字符串的长度。
示例代码如下:
```
size_t my_strlen(const char *str) {
size_t count = 0; while (*str != '\0') {
count++;
str++;
}
return count;
}
```
相关问题:
相关问题
大整数的加减乘除运算,使用C语言手动实现完整代码
以下是大整数的加减乘除运算的C语言手动实现完整代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
void reverse(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
void add(char* a, char* b, char* result) {
int carry = 0;
int i = 0, j = 0, k = 0;
int len_a = strlen(a), len_b = strlen(b);
while (i < len_a || j < len_b) {
int x = (i < len_a) ? (a[i] - '0') : 0;
int y = (j < len_b) ? (b[j] - '0') : 0;
int sum = x + y + carry;
result[k++] = (sum % 10) + '0';
carry = sum / 10;
i++; j++;
}
if (carry) {
result[k++] = carry + '0';
}
result[k] = '\0';
reverse(result);
}
void subtract(char* a, char* b, char* result) {
int borrow = 0;
int i = 0, j = 0, k = 0;
int len_a = strlen(a), len_b = strlen(b);
while (i < len_a || j < len_b) {
int x = (i < len_a) ? (a[i] - '0') : 0;
int y = (j < len_b) ? (b[j] - '0') : 0;
int diff = x - y - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
result[k++] = diff + '0';
i++; j++;
}
while (k > 1 && result[k - 1] == '0') {
k--;
}
result[k] = '\0';
reverse(result);
}
void multiply(char* a, char* b, char* result) {
int len_a = strlen(a), len_b = strlen(b);
int* temp = new int[len_a + len_b]();
for (int i = len_a - 1; i >= 0; i--) {
for (int j = len_b - 1; j >= 0; j--) {
int x = a[i] - '0';
int y = b[j] - '0';
temp[i + j + 1] += x * y;
}
}
for (int i = len_a + len_b - 1; i > 0; i--) {
temp[i - 1] += temp[i] / 10;
temp[i] %= 10;
}
int k = 0;
while (k < len_a + len_b && temp[k] == 0) {
k++;
}
for (int i = k; i < len_a + len_b; i++) {
result[i - k] = temp[i] + '0';
}
result[len_a + len_b - k] = '\0';
if (strlen(result) == 0) {
strcpy(result, "0");
}
delete[] temp;
}
void divide(char* a, char* b, char* result) {
int len_a = strlen(a), len_b = strlen(b);
if (len_a < len_b || (len_a == len_b && strcmp(a, b) < 0)) {
strcpy(result, "0");
return;
}
char* temp = new char[len_a + 1];
char* quotient = new char[len_a + 1];
strcpy(temp, a);
for (int i = 0; i < len_a - len_b + 1; i++) {
strcpy(quotient, "0");
while (strcmp(temp, b) >= 0) {
subtract(temp, b, temp);
add(quotient, "1", quotient);
}
reverse(quotient);
result[i] = quotient[0];
memmove(temp + 1, temp, strlen(temp) + 1);
temp[0] = '0';
}
result[len_a - len_b + 1] = '\0';
while (strlen(result) > 1 && result[0] == '0') {
memmove(result, result + 1, strlen(result));
}
delete[] temp;
delete[] quotient;
}
int main() {
char a[MAX_LEN], b[MAX_LEN], result[MAX_LEN];
printf("Enter the first number: ");
scanf("%s", a);
printf("Enter the second number: ");
scanf("%s", b);
add(a, b, result);
printf("Sum = %s\n", result);
subtract(a, b, result);
printf("Difference = %s\n", result);
multiply(a, b, result);
printf("Product = %s\n", result);
divide(a, b, result);
printf("Quotient = %s\n", result);
return 0;
}
```
希望这个代码能够帮助你实现大整数的加减乘除运算。
c语言中怎么实现
好的,如果您想要在 C 语言中实现统计自定义函数个数并计算平均行数的功能,可以参考下面的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN 1024 // 最大行长度
#define MAX_FUNC_NUM 100 // 最大函数数
int count_lines(char *filename, int start_line, int end_line);
int match_func(char *line, char *func_name, int *func_start);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
exit(1);
}
char *filename = argv[1];
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Failed to open file %s\n", filename);
exit(1);
}
char line[MAX_LINE_LEN];
char func_name[MAX_LINE_LEN];
int func_start = 0;
int func_lines[MAX_FUNC_NUM];
int func_count = 0;
int total_lines = 0;
while (fgets(line, MAX_LINE_LEN, fp)) {
int len = strlen(line);
if (len > 0 && line[len - 1] == '\n') {
line[len - 1] = '\0'; // 去掉行末的换行符
}
if (match_func(line, func_name, &func_start)) {
// 匹配到函数名,记录函数开始行号
func_lines[func_count] = func_start;
func_count++;
continue;
}
total_lines++;
}
fclose(fp);
// 计算各个函数的行数
int func_total_lines = 0;
for (int i = 0; i < func_count; i++) {
int start_line = func_lines[i];
int end_line = start_line;
while (end_line < total_lines && !match_func(line, func_name, &func_start)) {
end_line++;
fgets(line, MAX_LINE_LEN, fp);
}
func_total_lines += count_lines(filename, start_line, end_line - 1);
}
// 计算平均行数
double avg_lines = func_count > 0 ? (double)func_total_lines / func_count : 0;
printf("Total functions: %d\n", func_count);
printf("Average lines per function: %.2f\n", avg_lines);
return 0;
}
// 计算文件中特定行号范围内的代码行数
int count_lines(char *filename, int start_line, int end_line) {
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Failed to open file %s\n", filename);
exit(1);
}
int line_num = 1;
int line_count = 0;
char line[MAX_LINE_LEN];
while (fgets(line, MAX_LINE_LEN, fp)) {
if (line_num >= start_line && line_num <= end_line) {
// 如果当前行在范围内,统计代码行数
int len = strlen(line);
int is_code_line = 0;
for (int i = 0; i < len; i++) {
if (line[i] != ' ' && line[i] != '\t' && line[i] != '\r' && line[i] != '\n') {
is_code_line = 1;
break;
}
}
if (is_code_line) {
line_count++;
}
}
line_num++;
}
fclose(fp);
return line_count;
}
// 判断当前行是否为函数定义行,如果是,返回函数名和函数开始行号
int match_func(char *line, char *func_name, int *func_start) {
int len = strlen(line);
if (len < 3 || line[len - 2] != ')' || line[len - 1] != '{') {
// 不是函数定义行
return 0;
}
// 去掉行末的 '{' 符号
line[len - 1] = '\0';
// 查找函数名
int pos = len - 3;
while (pos >= 0 && line[pos] != ' ') {
pos--;
}
strcpy(func_name, line + pos + 1);
// 记录函数开始行号
*func_start = 1;
fseek(stdin, 0, SEEK_SET);
char buf[MAX_LINE_LEN];
while (fgets(buf, MAX_LINE_LEN, stdin)) {
if (strcmp(buf, line) == 0) {
break;
}
(*func_start)++;
}
return 1;
}
```
这个程序会读取指定的 C 语言代码文件,统计其中的自定义函数个数,并计算出这些函数的平均行数。具体实现的思路和 Python 版本类似,不同之处在于 C 语言需要手动读取文件和处理字符串,需要更多的细节处理。程序中使用了一些 C 语言的库函数,比如 fgets() 和 strcmp(),如果您不熟悉这些函数的使用,可以参考 C 语言的相关教程。
相关推荐
![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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)