1、sscanf()
2、sprintf()
原型:int sprintf( char *buffer, const char *format, [ argument] … );
#include
<stdio.h>//某個頭文件 int main
() /*主函數“整數”類型*/ { char buffer
[50]; /*“字符”類型的數組,下面共有50個元素。*/ int n,
a=5, b=3; /*三個變量都爲“整數”類型*/ n= sprintf (buffer, "%d
plus %d is %d" ,
a, b, a+b); /*賦予數值*/ printf ( "[%s]
is a string %d chars long\n" ,buffer,n); /*“格式輸出”*/ return 0; /*“返回
零” 也就是程序正常退出*/ } |
[5 plus 3 is 8] is a string 13 chars long
|
3、strstr()
從字符串str1中查找是否有字符串str2,如果有,從str1中的str2位置起,返回str1中str2起始位置的指針,如果沒有,返回null。
char str[]= "1234
xyz" ; char *
str1= strstr (str, "34" ); cout<<str1<<endl; |
4、itoa()
itoa是廣泛應用的非標準C語言擴展函數。由於它不是標準C語言函數,所以不能在所有的編譯器中使用。但是,大多數的編譯器(如Windows上的)通常在<stdlib.h>頭文件中包含這個函數。功能:將任意類型的數字轉換爲字符串。在<stdlib.h>中與之有相反功能的函數是atoi。
char *itoa( int value, char *string, int radix); |
#include
<stdlib.h>
#include
<stdio.h>
int
main()
{
int
number
= 123456;
char
string[25];
itoa(number,
string, 10);
printf
(
"integer
= %d string = %s\n"
,
number, string);
return
0;
}
5、atoi()
6、C++的substr()(string 類型)
#include
<string> #include
<iostream> using namespace std; int main(
) { string
str1 ( "Heterological
paradoxes are persistent." ); cout
<< "The
original string str1 is: "
<< endl; cout
<< str1 << endl; basic_string
< char >
str2 = str1.substr ( 6 , 7 ); cout
<< "The
substring str1 copied is: "
<< str2 << endl; basic_string
< char >
str3 = str1.substr ( ); cout
<< "The
default substring str3 is: "
<< endl; cout
<< str3 << endl; cout
<< "which
is the entire original string."
<< endl; return 0; } |
7、C++的append()(string 類型)
8、strcpy() / strncpy() / strcat() /
2、cin.getline(字符數組,數組長度,結束標記) 它從終端接受一個包含任意字符的字符串,直到遇到了指定的結束標記或到達了數組長度減1(因爲字符串最後一個字符必須是‘\0’,必須爲\0預留空間,結束標記符也可以不指定,此時默認回車爲結束標記)
3、字符處理函數頭文件<cstring> ------ 這些都是針對char ch[]或char *ch
strcpy(dst,src) 將字符串從src複製到dst,函數的返回值是dst的地址 ======= strcpy(const char*,const char*)
strnpy(dst,src,n) 至多從src複製n個字符到dst,函數返回值爲dst的地址
strcat(dst,src) 將src拼接到dst後,返回dst的地址
strncat(dst,src,n) 從src至多取n個字符拼接到dst後,返回值是dst的地址
strlen(s) 返回字符串s的長度,即字符串中的字符個數 ========== strlen(const char*)
strcmp(s1,s2) 比較s1和s2,如果s1>s2返回整數,s1==s2返回0,s1<s2返回負數
strncmp(s1,s2,n) 與上面類似,但至多比較n個字符
strchr(s,ch) 返回一個指向s中第一個出現字符ch的地址
strrchr(s,ch) 返回一個指向s中最後一次出現字符ch的地址
strstr(s1,s2) 返回一個指向s1中第一次出現字符串s2的地址
char* ch 和char ch1[] 的長度都可以用strlen(ch|ch1)來求得,但長度不包括‘\0’了,不過sizeof(ch1)的結果就包括‘\0’了 strlen(const char*)
4、extern 可以調用別的文件中的全局變量,但如果此用static修飾的變量只能用於本源文件,不能用extern被別的源文件調用
5、const int *p=&x :是一個指向的內容是常量的指針變量,由於使用了const說明指針指向的內容是一個常量,因而不能修改它指向的內容,如*p=30;就會報錯“左值是一個常量” ,但由於p是一個 普通的指針變量,我們可以讓指針指向另一個地址,列如p=&y
6、int *const p=&x :指針本身是一個常量,他固定指向某一變量,因此它本身的值不能變,但他指向的地址中的值是可變的 *p=30是對的,但p=&y是錯誤的