C语言字符串及其函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> int main(){ printf("输入一个整数"); int n; scanf("%d",&n); char a[n]; getchar(); for(int i=0;i<n;i++){ scanf("%c",&a[i]); } a[n]='\0'; printf("%s",a); }
strlen函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> //最后一位还要存储一个'/0' int main(){ char str[]="大家好才是正的哈"; printf("%s\n",str); printf("sizeiof的输出:%d",sizeof(str)); printf("strlen的输出%u",strlen(str)); }
strcpy
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> int main() { char str1[]="I love China"; char str2[]="I love girl"; char str3[50]; strcpy(str1,str2);//打印到出现'/0'的时候读取停止 strcpy(str3,"copy successful"); printf("str1=%s\n",str1); printf("str2=%s\n",str2); printf("str3=%s\n",str3); }
strncpy
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> int main() { char str1[]="IloveChina"; char str2[]="qweedadafc"; char str3[50]; strncpy(str1,str2,7);//控制到拷贝的 str1[7]='\0'; strcpy(str3,"copy successful"); printf("str1=%s\n",str1); printf("str2=%s\n",str2); printf("str3=%s\n",str3); }
strcat
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> //字符串的拼接 //strncat会自动添加结束符 int main() { char str1[]="I love China"; char str2[]="I love girl"; strcat(str1," "); strcat(str1,str2);//拷贝到出现'/0'的时候读取停止 printf("str1=%s\n",str1); printf("str2=%s\n",str2); }
strcmp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> int main() //比较字符串 { char str1[]="dalao"; char str2[]="dalao"; if(!strcmp(str1,str2)){ printf("它们相等"); }else{ printf("它们不等"); } }
对于函数的实现;
拷贝字符串 —— strcpy 和 strncpy 函数,
#include<iostream> using namespace std; #define MAX 1024 int main(){ char str1[MAX]; char str2[MAX]; char *target1=str1; char *target2=str2; printf("输入一个字符串到str1中"); fgets(str1,MAX,stdin); printf("开始拷贝str1内容到str2\n"); while((*target2++=*target1++)!='\0'){ } printf("拷贝完毕"); printf("%s",str2); }
#include <stdio.h> #define MAX 1024 int main() { char str1[MAX]; char str2[MAX]; char *target1 = str1; char *target2 = str2; char ch; int n; printf("请输入一个字符串到 str1 中:"); fgets(str1, MAX, stdin); printf("请输入需要拷贝的字符个数:"); scanf("%d", &n); printf("开始拷贝 str1 的内容到 str2 中...\n"); while (n--) { ch = *target2++ = *target1++; if (ch == '\0') { break; } if ((int)ch < 0) { *target2++ = *target1++; *target2++ = *target1++; } } *target2 = '\0'; printf("拷贝完毕!\n"); printf("现在,str2 中的内容是:%s\n", str2); return 0; }
连接字符串 —— strcat 和 strncat 函数
#include<iostream> using namespace std; #define MAX 1024 int main(){ char str1[2*MAX];//确保连接后不越界 char str2[MAX]; char *target1=str1; char *target2=str2; printf("输入第一个字符串") ; fgets(str1,MAX,stdin); printf("请输入第二个字符"); fgets(str2,MAX,stdin); //将指针指向第一个字符的末尾处 while(*target1++!='\0'); target1-=2;//去掉/0 与/n //连接字符串 while((*target1++=*target2++)!='\0'); printf("%s",str1); }
比较字符串 —— strcmp 和 strncmp 函数R+o
#include <stdio.h> #define MAX 1024 int main() { char str1[MAX]; char str2[MAX]; char *target1 = str1; char *target2 = str2; int index = 1; printf("请输入第一个字符串:"); fgets(str1, MAX, stdin); printf("请输入第二个字符串:"); fgets(str2, MAX, stdin); while (*target1 != '\0' && *target2 != '\0') { if (*target1++ != *target2++) { break; } index++; } if (*target1 == '\0' && *target2 == '\0') { printf("两个字符串完全一致!\n"); } else { printf("两个字符串不完全相同,第 %d 个字符出现不同!\n", index); } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #define MAX 1024 int main() { char str1[MAX]; char str2[MAX]; char *target1 = str1; char *target2 = str2; char ch; int index = 1, n; printf("请输入第一个字符串:"); fgets(str1, MAX, stdin); printf("请输入第二个字符串:"); fgets(str2, MAX, stdin); printf("请输入需要对比的字符个数:"); scanf("%d", &n); while (n && *target1 != '\0' && *target2 != '\0') { ch = *target1; if (ch < 0) { if (*target1++ != *target2++ || *target1++ != *target2++) { break; } } if (*target1++ != *target2++) { break; } index++; n--; } if ((n == 0) || (*target1 == '\0' && *target2 == '\0')) { printf("两个字符串的前 %d 个字符完全相同!\n", index); } else { printf("两个字符串不完全相同,第 %d 个字符出现不同!\n", index); } return 0; }