C语言中strcmp使用注意事项
时间: 2023-12-20 11:31:59 浏览: 61
在C语言中,strcmp函数用于比较两个字符串是否相等。使用该函数时需要注意以下几点:
1. strcmp函数的返回值为0时表示两个字符串相等,为正数时表示第一个不相等字符在第一个字符串中的ASCII码值大于第二个字符串中的对应字符的ASCII码值,为负数时则相反。
2. 在使用strcmp函数时,需要保证两个字符串都以'\0'结尾,否则可能会导致程序崩溃或者出现不可预知的结果。
3. strcmp函数是区分大小写的,如果需要忽略大小写进行比较,可以使用strcasecmp函数。
4. 如果需要比较两个字符串的前n个字符是否相等,可以使用strncmp函数。
下面是一个使用strcmp函数比较两个字符串是否相等的例子:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "hello";
char str2[20] = "world";
int result = strcmp(str1, str2);
if (result == 0) {
printf("str1和str2相等\n");
} else if (result > 0) {
printf("str1大于str2\n");
} else {
printf("str1小于str2\n");
}
return 0;
}
```
相关问题
修改文件的文件名后缀c语言
修改文件名后缀可以使用C语言中的文件操作函数`rename()`。具体实现步骤如下:
1. 使用`rename()`函数修改文件名后缀。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int result = rename("file.txt", "file.dat");
if(result == 0)
{
printf("文件名修改成功!\n");
}
else
{
printf("文件名修改失败!\n");
}
return 0;
}
```
2. 如果要批量修改文件名后缀,可以使用`opendir()`和`readdir()`函数遍历目录下的所有文件,然后使用`rename()`函数修改文件名后缀。
```c
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
int main()
{
DIR *dir;
struct dirent *ptr;
char path[1024] = "./"; // 文件所在路径
char old_ext[10] = ".txt"; // 原文件后缀
char new_ext[10] = ".dat"; // 新文件后缀
dir = opendir(path);
if(dir == NULL)
{
printf("打开目录失败!\n");
return -1;
}
while((ptr = readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
{
continue;
}
else
{
char *dot = strrchr(ptr->d_name, '.'); // 获取文件后缀
if(dot != NULL && strcmp(dot, old_ext) == 0)
{
char old_name[1024] = "";
char new_name[1024] = "";
sprintf(old_name, "%s%s", path, ptr->d_name); // 原文件名
strncpy(new_name, old_name, strlen(old_name) - strlen(old_ext)); // 新文件名
strcat(new_name, new_ext); // 添加新后缀
int result = rename(old_name, new_name); // 修改文件名
if(result == 0)
{
printf("文件名修改成功:%s -> %s\n", old_name, new_name);
}
else
{
printf("文件名修改失败:%s -> %s\n", old_name, new_name);
}
}
}
}
closedir(dir);
return 0;
}
```
注意事项:
1. 使用`rename()`函数修改文件名后缀时,要保证新的文件名不存在,否则会修改失败;
2. 在使用`readdir()`函数遍历目录下的所有文件时,要注意过滤掉`.`和`..`两个目录项;
3. 在使用`strrchr()`函数获取文件后缀时,要注意判断文件是否含有后缀,否则会出现异常。
飞花令C语言代码,详细
好的,以下是飞花令的C语言代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char sentence[100]; // 定义一个字符串数组,用于存储输入的句子
char last_word[20] = ""; // 定义一个字符串,用于存储上一个单词
char current_word[20]; // 定义一个字符串,用于存储当前单词
char first_letter; // 定义一个字符,用于存储当前单词的首字母
int count = 0; // 定义一个计数器,用于记录符合要求的单词的个数
printf("请输入一句话:");
fgets(sentence, 100, stdin); // 从标准输入读取一行字符串
char *word = strtok(sentence, " ,.?!"); // 使用 strtok 函数分割字符串
while (word != NULL) {
strcpy(current_word, word); // 复制当前单词到 current_word 字符串中
first_letter = current_word[0]; // 获取当前单词的首字母
if (strcmp(current_word, last_word) != 0 && first_letter == 'C') { // 判断当前单词是否符合要求
count++; // 计数器加一
printf("%d. %s\n", count, current_word); // 输出符合要求的单词
}
strcpy(last_word, current_word); // 将 current_word 字符串复制到 last_word 字符串中,以备下次使用
word = strtok(NULL, " ,.?!"); // 继续分割字符串
}
if (count == 0) { // 如果没有符合要求的单词,则输出提示信息
printf("没有符合要求的单词!\n");
}
return 0;
}
```
程序执行过程如下:
1. 定义一个字符串数组 `sentence`,用于存储输入的句子。
2. 定义一个字符串 `last_word`,用于存储上一个单词。
3. 定义一个字符串 `current_word`,用于存储当前单词。
4. 定义一个字符 `first_letter`,用于存储当前单词的首字母。
5. 定义一个计数器 `count`,用于记录符合要求的单词的个数。
6. 使用 `fgets` 函数从标准输入读取一行字符串,并存储到 `sentence` 中。
7. 使用 `strtok` 函数分割 `sentence` 字符串,获取第一个单词。
8. 如果当前单词与上一个单词不相同,并且当前单词的首字母是 `C`,则将计数器加一,并输出当前单词。
9. 将当前单词复制到 `last_word` 中,以备下次使用。
10. 继续使用 `strtok` 函数分割字符串,获取下一个单词。
11. 如果没有符合要求的单词,则输出提示信息。
12. 程序结束。
注意事项:
1. `fgets` 函数用于从标准输入读取一行字符串,其第二个参数指定了要读取的最大字符数,第三个参数指定了要读取的文件指针,这里使用 `stdin` 表示标准输入。
2. `strtok` 函数用于分割字符串,其第一个参数指定要分割的字符串,第二个参数指定分割字符串的分隔符,返回值为分割后的第一个子字符串的指针,当没有子字符串时返回 NULL。注意,该函数会改变原字符串的内容,如果需要保留原字符串,可以使用 `strdup` 函数复制一份。
3. `strcmp` 函数用于比较两个字符串是否相同,如果相同返回 0,否则返回非零值。
4. `strcpy` 函数用于将一个字符串复制到另一个字符串中,其第一个参数为目标字符串,第二个参数为源字符串。
5. 在输出符合要求的单词时,使用了 `%d` 和 `%s` 格式控制符,分别表示输出整数和字符串。在输出时,使用了 `printf` 函数,其第一个参数为格式字符串,后面的参数为要输出的变量。
6. 在最后输出提示信息时,使用了 `\n` 换行符,表示换行。