1、strlen函數:
作用:求字符串長度。
原型(用法):strlen(const char s[]).。
C語言代碼實現strlen函數:
#include <stdio.h>
int strle(char a[])
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
int main()//測試使用的主函數
{
char arr[200]="sgsdlfjlsdj";
printf("%d\n",strle(arr));
}
2、strcpy函數:
作用:複製字符串,將後面的字符串內容複製到前面字符串內。
原型(用法):strcpy(char s[],const char t[])。
C語言代碼實現strcpy函數:
#include <stdio.h>
void strcp(char a[],char b[])
{
int i;
for(i=0;a[i]!='\0';i++)
{
b[i]=a[i];
}
b[i]='\0';
}
int main()
{
char a[200]="dsgsdlf";
char b[200];
int i;
strcp(a,b);
printf("原本數組:%s\n",a);
printf("複製出來的數組:%s\n",b);
}
3、strcmp函數:
作用:比較兩個字符串。
原型(用法):strcmp(const char s1[],char s2[]);
C語言代碼實現strcmp函數:
#include <stdio.h>
int strcm(char a[],char b[])
{
int i;
for(i=0;a[i]!='\0'||b[i]!='\0';i++)
{
if(a[i]>b[i])
return 1;
if(b[i]>a[i])
return -1;
}
if(a[i]=='\0'&&b[i]=='\0')
return 0;
}
int main()
{
char a[200]="Aheloo";
char b[200]="Ahello";
printf("%d",strcm(a,b));
}
4、strcat函數:
作用:鏈接字符串,將後面字符串首部鏈接到前面字符串尾部。
原型(用法):strcat(char s[],const char t[])。
C語言代碼實現strcat函數:
#include <stdio.h>
void strca(char a[],char b[])
{
int i,k=0;
for(i=0;a[i]!='\0';i++)
{
k++;
}
for(i=0;b[i]!='\0';i++)
{
a[k+i]=b[i];
}
}
int main()
{
char a[200]="abcd";
char b[]="efgh";
strca(a,b);
printf("%s",a);
}
注:在使用這些函數之前應寫入string.h頭文件