#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
void main(){
char a[] = "i think u a superman";
char *p = a;
*(p + 7) ='\0';
printf("%p\n", p + 2);
printf("%s",p+2);
Sleep(55555555);
return;
}
strncpy(p, p1, n) 複製指定長度字符串
strncat(p, p1, n) 附加指定長度字符串
strlen(p) 取字符串長度
strcmp(p, p1) 比較字符串
strchr(p, c) 在字符串中查找指定字符
strrchr(p, c) 在字符串中反向查找
strstr(p, p1) 查找字符串
strpbrk(p, p1) 以目標字符串的所有字符作爲集合,在當前字符串查找該集合的任一元素
strspn(p, p1) 以目標字符串的所有字符作爲集合,在當前字符串查找不屬於該集合的任一元素的偏移
strcspn(p, p1) 以目標字符串的所有字符作爲集合,在當前字符串查找屬於該集合的任一元素的偏移
* 具有指定長度的字符串處理函數在已處理的字符串之後填補零結尾符
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(){
char ch[100];
char ab[100];
printf("請輸入一個字符串:");
scanf("%s",ch);
strcpy(ab, ch);
<span style="white-space:pre"> </span> <pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(){
char ch1[20] = "X";
char ch2[20] = "A";
printf("%d\n",strcmp(ch1,ch2)); //字符串比較是否相等
system("pause");
return;
}
//複製字符串printf("11111\n%s",ab);system("pause");return;}
原型:strlen( const char string[] );
功能:統計字符串string中字符的個數
例程:
#include <iostream.h>
#include <string.h>
void main(void)
{
char str[100];
cout <<"請輸入一個字符串:";
cin >>str;
cout <<"The length of the string is :"<<strlen(str)<<"個"<<endl;
}
運行結果The length of the string is x (x爲你輸入的字符總數字)
注意:strlen函數的功能是計算字符串的實際長度,不包括'\0'在內。另外,strlen函數也可以直接測試字符串常量的長度,如:strlen("Welcome")。
上面都是字符串的基本操作,字符串的截取也與其他數組的截取方式相同,可以先定義一個指針預先指針第幾個元素,後面+到某元素逐一打印出來也是可以的("%s",ch)。