一焙贷、 字符串操作
頭文件:
#include <string.h>
#include <ctype.h>
1. 從鍵盤接收字符串,長度為size
fgets(char[] buf, int size, stdin);
注意:得到的字符串最后是以0結(jié)尾歹叮,占用一個空間
2. 字符串處理跑杭,小寫轉(zhuǎn)化為大寫
if(isalpha(buf[i]))
buf[i]=toupper(buf[i]);
3. 判斷字符串中是否有相等字符
strchr(s, buf[i])
4. 以某個順序?qū)⒆址4嫦?/h2>
sprintf(buf, "%d%d%d%d%d", a,b,c,d,e);
二、函數(shù)和遞歸
1. 數(shù)學(xué)函數(shù)
判斷素數(shù)
#include <math.h>
#include <assert.h>
int is_prime(int x)
{
int i,m;
// 宏定義咆耿,如果不滿足條件德谅,直接中斷執(zhí)行
assert(x>=0);
if(x==1)return 0;
//floor取整,開根號計算素數(shù)即可
m=floor(sqrt(x)+0.5);
for(i=2;i<=m;i++)
if(x%i==0)return 0;
return 1;
}
2. 指針
注意參數(shù)前面加&表示的是變量的地址
sprintf(buf, "%d%d%d%d%d", a,b,c,d,e);
#include <math.h>
#include <assert.h>
int is_prime(int x)
{
int i,m;
// 宏定義咆耿,如果不滿足條件德谅,直接中斷執(zhí)行
assert(x>=0);
if(x==1)return 0;
//floor取整,開根號計算素數(shù)即可
m=floor(sqrt(x)+0.5);
for(i=2;i<=m;i++)
if(x%i==0)return 0;
return 1;
}
注意參數(shù)前面加&表示的是變量的地址
錯誤寫法:(當(dāng)t指向的空間只讀的時候萨螺,將發(fā)生錯誤)
void swap(int*a, int*b)
{
int *t;
*t = *a;
*a = *b;
*b = *t;
}
3. 堆棧
使用遞歸時要注意防止段溢出慰技。
可執(zhí)行文件中,正文段存儲指令掏颊,數(shù)據(jù)段存儲已初始化的全局變量手报,BSS段存儲未賦值的全局變量所需的空間掩蛤。
建議把較大的數(shù)組放在main函數(shù)外陈肛,也就是把數(shù)組存放在正文段