㈠ c語言中string怎麼用啊
C語言提供了豐富的字元串處理函數, 大致可分為字元串的輸入、輸出、合並、修改、比較、轉換、復制、搜索幾類。 使用這些函數可大大減輕編程的負擔。用於輸入輸出的字元串函數, 在使用前應包含頭文件"stdio.h" ; 使用其它字元串函數則應包含頭文件"string.h"。 下面介紹幾個最常用的字元串函數。
1.字元串輸出函數 puts 格式: puts (字元數組名) 功能:把字元數組中的字元串輸出到顯示器。 即在屏幕上顯示該字元串
#include"stdio.h"
main()
{
static char c[]="BASIC\ndBASE";
puts(c);
}
2.字元串輸入函數gets 格式: gets (字元數組名) 功能:從標准輸入設備鍵盤上輸入一個字元串。 本函數得到一個函數值,即為該字元數組的首地址。
#include"stdio.h"
main()
{
char st[15];
printf("input string:\n");
gets(st);
puts(st);
}
3.字元串連接函數strcat 格式: strcat (字元數組名1,字元數組名2) 功能:把字元數組2中的字元串連接到字元數組1 中字元串的後面,並刪去字元串1後的串標志「\0」。本函數返回值是字元數組1的首地址。
#include"string.h"
main()
{
static char st1[30]="My name is ";
int st2[10];
printf("input your name:\n");
gets(st2);
strcat(st1,st2);
puts(st1);
}
4.字元串拷貝函數strcpy 格式: strcpy (字元數組名1,字元數組名2) 功能:把字元數組2中的字元串拷貝到字元數組1中。串結束標志「\0」也一同拷貝。字元數名2, 也可以是一個字元串常量。這時相當於把一個字元串賦予一個字元數組。
#include"string.h"
main()
{
static char st1[15],st2[]="C Language";
strcpy(st1,st2);
puts(st1);printf("\n");
}
5.字元串比較函數strcmp 格式: strcmp(字元數組名1,字元數組名2) 功能:按照ASCII碼順序比較兩個數組中的字元串,並由函數返回值返回比較結果。
字元串1=字元串2,返回值=0;
字元串2〉字元串2,返回值〉0;
字元串1〈字元串2,返回值〈0。
本函數也可用於比較兩個字元串常量,或比較數組和字元串常量。
#include"string.h"
main()
{ int k;
static char st1[15],st2[]="C Language";
printf("input a string:\n");
gets(st1);
k=strcmp(st1,st2);
if(k==0) printf("st1=st2\n");
if(k>0) printf("st1>st2\n");
if(k<0) printf("st1<st2\n");
}
6.測字元串長度函數strlen 格式: strlen(字元數組名) 功能:測字元串的實際長度(不含字元串結束標志『\0』) 並作為函數返回值。
#include"string.h"
main()
{ int k;
static char st[]="C language";
k=strlen(st);
printf("The lenth of the string is %d\n",k);
}
㈡ c語言string的用法大全
C語言是一門面向過程的、抽象化的通用程序設計語言,廣泛應用於底層開發。C語言能以簡易的方式編譯、處理低級存儲器。C 語言string的用法有哪些呢,請看看下面我為你整理 總結 的c語言string的用法大全_C語言中string使用 方法 。
c語言string的用法
函數原型:char *strp(const char *s)
函數功能:字元串拷貝,目的空間由該函數分配
函數返回:指向拷貝後的字元串指針
參數說明:src-待拷貝的源字元串
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
#include<alloc.h>
intmain()
{
char*p_str,*string="abcde";
p_str=strp(string);
printf("%s",p_str);
free(p_str);
return0;
}
@函數名稱:strcpy
函數原型:char* strcpy(char* str1,char* str2);
函數功能:把str2指向的字元串拷貝到str1中去
函數返回:返回str1,即指向str1的指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
charstring[10];
char*str1="abcdefghi";
strcpy(string,str1);
printf("thestringis:%s ",string);
return0;
}
@函數名稱:strncpy
函數原型:char *strncpy(char *dest, const char *src,intcount)
函數功能:將字元串src中的count個字元拷貝到字元串dest中去
函數返回:指向dest的指針
參數說明:dest-目的字元串,src-源字元串,count-拷貝的字元個數
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
char*src="bbbbbbbbbbbbbbbbbbbb";//20'b's
chardest[50]="aaaaaaaaaaaaaaaaaaaa";//20'a's
puts(dest);
strncpy(dest,src,10);
puts(dest);
return0;
}
輸出:
[cpp] view plain
/*******************************************
aaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbaaaaaaaaaa
*******************************************/
注意:strncpy只復制指定長度的字元,不會自動在末尾加''。若指定長度超過源字元串長度,不夠的部分補『』,
@函數名稱:strcat
函數原型:char* strcat(char * str1,char * str2);
函數功能:把字元串str2接到str1後面,str1最後的''被取消
函數返回:str1
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
charbuffer[80];
strcpy(buffer,"Hello");
strcat(buffer,"world");
printf("%s ",buffer);
return0;
}
@函數名稱:strncat
函數原型:char *strncat(char *dest, const char *src, size_t maxlen)
函數功能:將字元串src中前maxlen個字元連接到dest中
函數返回:
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
charbuffer[80];
intmain()
{
strcpy(buffer,"Hello");
strncat(buffer,"world",8);
printf("%s ",buffer);
strncat(buffer,"*************",4);
printf("%s ",buffer);
return0;
}
注意:與strncpy不同的是,strncat會自動在末尾加『』,若指定長度超過源字元串長度,則只復制源字元串長度即停止
@函數名稱:strcmp
函數原型:int strcmp(char * str1,char * str2);
函數功能:比較兩個字元串str1,str2.
函數返回:str1<str2,返回負數;str1=str2,返回 0;str1>str2,返回正數.
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
char*buf1="aaa",*buf2="bbb",*buf3="ccc";
intptr;
ptr=strcmp(buf2,buf1);
if(ptr>0)
printf("buffer2isgreaterthanbuffer1 ");
else
printf("buffer2islessthanbuffer1 ");
ptr=strcmp(buf2,buf3);
if(ptr>0)
printf("buffer2isgreaterthanbuffer3 ");
else
printf("buffer2islessthanbuffer3 ");
return0;
}
@函數名稱:strncmp
函數原型:int strncmp(char *str1,char *str2,int count)
函數功能:對str1和str2中的前count個字元按字典順序比較
函數返回:小於0:str1<str2,等於0:str1=str2,大於0:str1>str2
參數說明:str1,str2-待比較的字元串,count-比較的長度
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
charstr1[]="aabbc";//
charstr2[]="abbcd";//
//為使測試程序更簡練,此處假定了strncmp只返回-1,0,1三個數
charres_info[]={'<','=','>'};
intres;
//前1個字元比較
res=strncmp(str1,str2,1);
printf("1:str1%cstr2 ",res_info[res+1]);
//前3個字元比較
res=strncmp(str1,str2,3);
printf("3:str1%cstr2 ",res_info[res+1]);
}
輸出:
[cpp] view plain
/****************************************
1:str1=str2
3:str1<str2
*****************************************/
@函數名稱:strpbrk
函數原型:char *strpbrk(const char *s1, const char *s2)
函數功能:得到s1中第一個「同時也出現在s2中」字元的位置指針
函數返回:位置指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
char*p="Findallvowels";
p=strpbrk(p+1,"aeiouAEIOU");
while(p)
{
printf("%s ",p);
p=strpbrk(p+1,"aeiouAEIOU");
}
return0;
}
輸出:
[cpp] view plain
/**************************************
indallvowels
allvowels
owels
els
**************************************/
@函數名稱:strcspn
函數原型:int strcspn(const char *s1, const char *s2)
函數功能:統計s1中從頭開始直到第一個「來自s2中的字元」出現的長度
函數返回:長度
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
printf("%d ",strcspn("abcbcadef","cba"));
printf("%d ",strcspn("xxxbcadef","cba"));
printf("%d ",strcspn("123456789","cba"));
return0;
}
輸出:
[cpp] view plain
/************************
0
3
9
************************/
@函數名稱:strspn
函數原型:int strspn(const char *s1, const char *s2)
函數功能:統計s1中從頭開始直到第一個「不來自s2中的字元」出現的長度
函數返回:位置指針
參數說明:
所屬文件:<string.h>
[html] view plain
#include<stdio.h>
#include<string.h>
#include<alloc.h>
intmain()
{
printf("%d ",strspn("abcbcadef","cba"));
printf("%d ",strspn("xxxbcadef","cba"));
printf("%d ",strspn("123456789","cba"));
return0;
}
輸出:
[cpp] view plain
/************************
6
0
0
************************/
@函數名稱:strchr
函數原型:char* strchr(char* str,char ch);
函數功能:找出str指向的字元串中第一次出現字元ch的位置
函數返回:返回指向該位置的指針,如找不到,則返回空指針
參數說明:str-待搜索的字元串,ch-查找的字元
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
char*str="Thisisastring!";
charch;
char*p;
while(1)
{
printf("Pleaseinputachar:");
ch=getchar();
p=strchr(str,ch);
if(p)
printf("%cisthe%dcharacterof"%s" ",ch,(int)(p-str+1),str);
else
printf("Notfound! ");
printf("PressESCtoquit! ");
if(27==getch())
break;
fflush(stdin);
}
return0;
}
運行結果:
[cpp] view plain
/********************************************
Pleaseinputachar:i
iisthe3characterof"Thisisastring!"
PressESCtoquit!
Pleaseinputachar:l
Notfound!
PressESCtoquit!
Pleaseinputachar:s
sisthe4characterof"Thisisastring!"
PressESCtoquit!
**********************************************/
@函數名稱:strrchr
函數原型:char *strrchr(const char *s, int c)
函數功能:得到字元串s中最後一個含有c字元的位置指針
函數返回:位置指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
charstring[15];
char*ptr,c='r';
strcpy(string,"Thisisastring");
ptr=strrchr(string,c);
if(ptr)
printf("Thecharacter%cisatposition:%d",c,ptr-string);
else
printf("Thecharacterwasnotfound");
return0;
}
@函數名稱:strstr
函數原型:char* strstr(char* str1,char* str2);
函數功能:找出str2字元串在str1字元串中第一次出現的位置(不包括str2的串結束符)
函數返回:返回該位置的指針,如找不到,返回空指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;
ptr=strstr(str1,str2);
printf("Thesubstringis:%s ",ptr);
return0;
}
輸出:
The substringis:Watcom C/C++
@函數名稱:strrev
函數原型:char *strrev(char *s)
函數功能:將字元串中的所有字元顛倒次序排列
函數返回:指向s的指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
charforward[]="string";//原文中定義為char*是不對的,指向代碼段的指針內容是不可變的
printf("Beforestrrev():%s",forward);
strrev(forward);
printf("Afterstrrev():%s",forward);
return0;
}
輸出:
[cpp] view plain
/************************************
Beforestrrev():string
Afterstrrev():gnirts
************************************/
@函數名稱:strnset
函數原型:char *strnset(char *s, int ch, size_t n)
函數功能:將字元串s中前n個字元設置為ch的值
函數返回:指向s的指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";
charletter='x';
printf("stringbeforestrnset:%s ",string);
strnset(string,letter,10);
printf("stringafterstrnset:%s ",string);
return0;
}
輸出:
[cpp] view plain
/*************************************************
stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa
stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa
*************************************************/
@函數名稱:strset
函數原型:char *strset(char *s, int ch)
函數功能:將字元串s中所有字元設置為ch的值
函數返回:指向s的指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
charstring[10]="123456789";
charsymbol='c';
printf("Beforestrset():%s",string);
strset(string,symbol);
printf("Afterstrset():%s",string);
return0;
}
@函數名稱:strtok
函數原型:char *strtok(char *s1, const char *s2)
函數功能:分解s1字元串為用特定分隔符分隔的多個字元串(一般用於將英文句分解為單詞)
函數返回:字元串s1中首次出現s2中的字元前的子字元串指針
參數說明:s2一般設置為s1中的分隔字元
規定進行子調用時(即分割s1的第二、三及後續子串)第一參數必須是NULL
在每一次匹配成功後,將s1中分割出的子串位置替換為NULL(摘下鏈中第一個環),因此s1被破壞了
函數會記憶指針位置以供下一次調用
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
char*p;
char*buffer;
char*delims={".,"};
buffer=strp("Findwords,allofthem.");
printf("%s ",buffer);
p=strtok(buffer,delims);
while(p!=NULL){
printf("word:%s ",p);
p=strtok(NULL,delims);
}
printf("%s ",buffer);
return0;
}//根據測試,可以隨時給strtok的第一個參數輸入一個新的字元串,開始新字元串的分隔
PS:根據測試,可以隨時給strtok的第一個參數輸入一個新的字元串,開始新字元串的分隔
@函數名稱:strupr
函數原型:char *strupr(char *s)
函數功能:將字元串s中的字元變為大寫
函數返回:
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//會影響原字元串的內存,用char[]來聲明
ptr=strupr(string);
printf("%s",ptr);
return0;
}
@函數名稱:strlwr
函數原型:char *strlwr(char *s)
函數功能:將字元串中的字元變為小寫字元
函數返回:指向s的指針
參數說明:
所屬文件:<string.h>
[cpp] view plain
#include<string.h>
intmain()
{
charstr[]="HOWTOSAY";
printf("%s",strlwr(str));
return0;
}
@函數名稱:strerror
函數原型:char *strerror(int errnum)
函數功能:得到錯誤信息的內容信息
函數返回:錯誤提示信息字元串指針
參數說明:errnum-錯誤編號
所屬文件:<string.h>
[cpp] view plain
#include<stdio.h>
#include<errno.h>
intmain()
{
char*buffer;
buffer=strerror(errno);
printf("Error:%s",buffer);
return0;
}
@函數名稱:memcpy
函數原型:void *memcpy(void *dest, const void *src, size_t n)
函數功能:字元串拷貝
函數返回:指向dest的指針
參數說明:src-源字元串,n-拷貝的最大長度
所屬文件:<string.h>,<mem.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
charsrc[]="******************************";
chardest[]="";
char*ptr;
printf("destinationbeforememcpy:%s ",dest);
ptr=memcpy(dest,src,strlen(src));
if(ptr)
printf("destinationaftermemcpy:%s ",dest);
else
printf("memcpyfailed");
return0;
}
輸出:
[cpp] view plain
/*************************************************************
destinationbeforememcpy:
destinationaftermemcpy:******************************456709
**************************************************************/
@函數名稱:memccpy
函數原型:void *memccpy(void *dest, const void *src, int c, size_t n)
函數功能:字元串拷貝,到指定長度或遇到指定字元時停止拷貝
函數返回:
參數說明:src-源字元串指針,c-中止拷貝檢查字元,n-長度,dest-拷貝底目的字元串指針
所屬文件:<string.h>,<mem.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
char*src="Thisisthesourcestring";
chardest[50];
char*ptr;
ptr=memccpy(dest,src,'c',strlen(src));
if(ptr)
{
*ptr='';
printf("Thecharacterwasfound:%s",dest);
}
else
printf("Thecharacterwasn'tfound");
return0;
}
輸出:
[cpp] view plain
/*****************************************
Thecharacterwasfound:Thisisthesourc
*****************************************/
PS:指定字元被復制到dest中,memccpy返回了dest中指定字元的下一處的地址,返回NULL表示未遇到指定字元
@函數名稱:memchr
函數原型:void *memchr(const void *s, int c, size_t n)
函數功能:在字元串中第開始n個字元中尋找某個字元c的位置
函數返回:返回c的位置指針,返回NULL時表示未找到
參數說明:s-要搜索的字元串,c-要尋找的字元,n-指定長度
所屬文件:<string.h>,<mem.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
charstr[17];
char*ptr;
strcpy(str,"Thisisastring");
ptr=memchr(str,'r',strlen(str));
if(ptr)
printf("Thecharacter'r'isatposition:%d",ptr-str);
else
printf("Thecharacterwasnotfound");
return0;
}
@函數名稱:memcmp
函數原型:int memcmp(const void *s1, const void *s2,size_t n)
函數功能:按字典順序比較兩個串s1和s2的前n個位元組
函數返回:<0,=0,>0分別表示s1<,=,>s2
參數說明:s1,s2-要比較的字元串,n-比較的長度
所屬文件:<string.h>,<mem.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memcmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)printf("not");
printf("thesame ");
return0;
}
@函數名稱:memicmp
函數原型:int memicmp(const void *s1, const void *s2, size_t n)
函數功能:按字典順序、不考慮字母大小寫對字元串s1,s2前n個字元比較
函數返回:<0,=0,>0分別表示s1<,=,>s2
參數說明:s1,s2-要比較的字元串,n-比較的長度
所屬文件:<string.h>,<mem.h>
[cpp] view plain
#include<stdio.h>
#include<string.h>
intmain()
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memicmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)printf("not");
printf("thesame");
return0;
}
輸出:
[cpp] view plain
/**************************************
***************************************/
@函數名稱:memmove
函數原型:void *memmove(void *dest, const void *src, size_t n)
函數功能:字元串拷貝
函數返回:指向dest的指針
參數說明:src-源字元串,n-拷貝的最大長度
所屬文件:<string.h>,<mem.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
intmain()
{
chardest[40]="";
printf("destinationpriortomemmove:%s ",dest);
memmove(dest+1,dest,35);
printf("destinationaftermemmove:%s",dest);
return0;
}
PS:與memcpy不同的是,memmove可以處理目的字元串與源字元串地址空間出現重疊的情況,可保證待復制的內容不被破壞。
@函數名稱: memset
函數原型: void *memset(void *s, int c, size_t n)
函數功能: 字元串中的n個位元組內容設置為c
函數返回:
參數說明: s-要設置的字元串,c-設置的內容,n-長度
所屬文件: <string.h>,<mem.h>
[cpp] view plain
#include<string.h>
#include<stdio.h>
#include<mem.h>
intmain()
{
charbuffer[]="Helloworld";
printf("Bufferbeforememset:%s/n",buffer);
memset(buffer,'*',strlen(buffer)-1);
printf("Bufferaftermemset:%s",buffer);
return0;
}
c語言string的用法大全相關 文章 :
★ c語言string的用法
★ c語言的用法
★ Linux C語言字元與字元串處理
★ c語言中strcmp的用法
★ c語言大括弧的用法
★ c語言位運算符的用法
★ c語言char的用法
★ c語言中sort的用法詳解
★ c語言中int的用法
★ c語言map的用法