字符串处理函数
在C语言中,字符串是一种重要的数据类型,它由字符数组组成。处理字符串时,我们可以使用一系列的函数来简化操作。以下是一些常见的字符串处理函数及其使用方法。
1. strlen() 函数
strlen() 函数用于计算字符串的长度,不包括结尾的空字符 \0。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of string: %d\n", strlen(str));
return 0;
}
2. strcpy() 函数
strcpy() 函数用于复制一个字符串到另一个字符串。
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
3. strcat() 函数
strcat() 函数用于连接两个字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
4. strcmp() 函数
strcmp() 函数用于比较两个字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
动态内存分配
在C语言中,动态内存分配是管理内存的一种方式。使用 malloc(), calloc(), 和 realloc() 函数,我们可以根据需要分配和调整内存。
1. malloc() 函数
malloc() 函数用于分配指定大小的内存块。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(10 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// 使用ptr数组
free(ptr); // 释放内存
return 0;
}
2. calloc() 函数
calloc() 函数用于分配指定大小的内存块,并将所有位初始化为零。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)calloc(10, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// 使用ptr数组
free(ptr); // 释放内存
return 0;
}
3. realloc() 函数
realloc() 函数用于重新分配内存块,并可能将其移动到新的位置。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(10 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// 修改ptr数组
ptr = (int*)realloc(ptr, 20 * sizeof(int));
if (ptr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
// 使用ptr数组
free(ptr); // 释放内存
return 0;
}
实战案例:模拟用户登录系统
以下是一个简单的用户登录系统示例,使用动态内存分配存储用户名和密码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *username, *password;
char inputUsername[50], inputPassword[50];
int found = 0;
// 假设的用户数据库
char *dbUsername = "user";
char *dbPassword = "pass";
printf("Enter username: ");
scanf("%49s", inputUsername);
printf("Enter password: ");
scanf("%49s", inputPassword);
// 动态分配内存以存储输入的用户名和密码
username = (char*)malloc(strlen(inputUsername) + 1);
password = (char*)malloc(strlen(inputPassword) + 1);
// 复制输入的用户名和密码到动态分配的内存
strcpy(username, inputUsername);
strcpy(password, inputPassword);
// 检查用户名和密码是否匹配
if (strcmp(username, dbUsername) == 0 && strcmp(password, dbPassword) == 0) {
printf("Login successful!\n");
found = 1;
} else {
printf("Login failed!\n");
}
// 释放动态分配的内存
free(username);
free(password);
return 0;
}
在这个例子中,我们首先定义了一个简单的用户数据库,然后提示用户输入用户名和密码。接着,我们使用 malloc() 函数动态分配内存以存储用户输入的用户名和密码,然后使用 strcpy() 函数复制输入的值。最后,我们比较输入的用户名和密码与数据库中的值,并根据比较结果显示相应的消息。
希望这些示例能帮助你更好地理解C语言编程。随着你的深入学习和实践,你会发现自己越来越熟练。祝你编程愉快!
