//字符數(shù)組,存儲字符的數(shù)組
char str1[7] = {'h','e','l','l','o','a','\0'};
//打印下標(biāo)為3的字符
printf("%c\n",str1[3]);
//將小寫的o改為大寫的O
str1[4] = 'O';
printf("%s \n",str1);
//定義字符串--
//字符串 本身就都是字符數(shù)組,只是字符串的結(jié)尾是以\0來結(jié)束的,如果在字符數(shù)組中也設(shè)置了\0那么此數(shù)組也可以當(dāng)做字符串來輸出
char str2[]="Hello World";
printf("%s \n",str2);
//來列舉字符串的情況
//1.設(shè)置元素個數(shù)大于實際元素的個數(shù)
char str3[10]={'r','s','y','q'};
printf("%s \n",str3);
//2.手動設(shè)置\0
char str4[] = {'h','e','\0'};
printf("%s \n",str4);
//##########字符串操作函數(shù)
//計算字符串長度,以\0結(jié)束的標(biāo)識
char string1[]="you are my love";
//注意長度屬于計算類型,但是>=0的 strlen只能用來計算字符串
unsigned long strLenght = strlen(string1);
// printf("%lu \n",strLenght);
//字符串拼接,將一個字符串拼接給另一個字符串
//字符串2的內(nèi)存必須要給大
char string2[30] = "Wuli xiao fan";
char string3[] = "World";
strcat(string2, string3);//將字符串3拼接到2后面
printf("%s \n",string2);
//長度不用考慮容量,但是字符串拼接和拷貝必須注意變量內(nèi)存問題
//3.字符串拷貝
strcpy(string2, string3);//后面的復(fù)制到前面
printf("%s \n",string2);
//4.比較兩個字符串對應(yīng)位置的字符,比較的是字符串的整數(shù)值,對應(yīng)位置的相減,結(jié)果不為0就結(jié)束比較
char s1[]="abcde";
char s2[]="bbcdf";
int result = strcmp(s1, s2);//前-后,,,一旦不一樣就停止
printf("差值為%d \n",result);
//練習(xí)1:已知一個字符串,計算出字符串中的空格的數(shù)量
char ss[]="slsl lplp lp lpp lpl";//32 回車10
unsigned long strLenght1 = strlen(ss);
int count=0;
for (int i=0; i<strLenght1; i++) {
if (ss[i] == ' ') {
count++;
}
}
printf("%d \n",count);
}
return 0;
//1.數(shù)組:幫助創(chuàng)建多個變量
//缺點:數(shù)組中只能存儲相同類型的數(shù)據(jù)
//數(shù)組定義的規(guī)則
//數(shù)據(jù)類型 數(shù)組名[元素個數(shù)] = {值1,值2,值3....}
//注意:數(shù)組的元素個數(shù)只能為整型常量,不要寫變量
//開始寫
int array[5] = {2,5,89,10,6};
char a[4] = {'i','h','k','l'}; // 4個字節(jié)
//2.可以不設(shè)置數(shù)組個數(shù),系統(tǒng)會按照數(shù)組的賦值自行分配
short b[] = {9,80,6,9};//默認(rèn)就是占8個字節(jié)的空間
//函數(shù)sizeof()可以幫助計算變量在內(nèi)存中占用的字節(jié) 用lu輸出 unsigned--(-127~128)無符號默認(rèn)正值
// 統(tǒng)計數(shù)據(jù) 無符號
//lu 無符號長整型 unsigned long
printf("%lu \n",sizeof(array));
printf("%lu \n",sizeof(b));
//制定數(shù)組元素的個數(shù),所給的數(shù)組賦的值得個數(shù)必須,小于等于元素個數(shù) 只要保證數(shù)組不越界
// int c[5] = {90,8,7,9,88,9,99};
// printf("%d\n",c);
//3.數(shù)組的下標(biāo)
//下標(biāo)從0開始,最大下標(biāo)就是元素個數(shù)減1
//下標(biāo)意義:就是幫助準(zhǔn)確找到某一個元素
//使用規(guī)則:數(shù)組名[元素下標(biāo)]
printf("%d \n",array[2]);
//修改
array[2] = 2222;
printf("%d \n",array[2]);
//數(shù)組的輸出,依賴循環(huán)
//通過循環(huán)遍歷數(shù)組的每一個下標(biāo),來取出數(shù)組的值
//注意 初始化變量值要從0開始
for (int i = 0; i < sizeof(array) / 4; i++) {
printf("&&%d***\n",array[i]);
}
//隨機(jī)數(shù)----arc4random() 中間數(shù):(大區(qū)間-小區(qū)間+1)+小區(qū)間
int num1 = arc4random()%(90-55+1)+55;
printf("隨機(jī)數(shù)為:%d\n",num1);
//定義整形數(shù)組,包含10個元素,元素的值范圍[30,70],數(shù)組的初始值是0.給數(shù)組賦隨機(jī)值,并求和還有平均數(shù)
int a1[10] = {0},sum = 0,arg=0.0;
for (int i = 0; i<10 ; i++) {
a1[i] = arc4random() % (70 - 30 + 1) + 30;
printf("數(shù)組%d \n",a1[i]);
sum += a1[i];
arg=sum / 10.0;
}
printf("和為%d \n 平均值%d \n",sum,arg);
//l2 有兩個元素的個數(shù)為10整型數(shù)組a和b(數(shù)組個自定義)數(shù)組a中的元素隨機(jī)賦值[20,40],然后將數(shù)組a的元素賦值到b中
int a2[10],b2[10];
for (int i = 0; i < 10; i++) {
a2[i] = arc4random() % (40-20+1) +20;
printf("a2 %d\n",a2[i]);
b2[i] = a2[i];
printf("b2 %d\n",b2[i]);
}
//輸出打印,數(shù)組之間不能相互賦值也不能相互打印
// for (int i = 0; i<10; i++) {
// printf("a2 %d %db2 \n",a2[i],b2[i]);
// }
//3.元素個數(shù)為10的整形數(shù)組,元素賦值隨機(jī)的范圍[30,70],求數(shù)組中的最小值和最大值
int a3[10],min=70,max=0;
// for (int i = 0; i<10; i++) {
// for (int j = 0; j<10; j++) {
// a3[i] = arc4random() % (70-30+1) + 30;
// a3[j] = a3[i+1];
// if (a3[i]>a[j]) {
// a[j] = a3[i];
// max = a[j];
//
// }
// }
//
// }
// printf("max===%d \n",max);
//
//
// for (int i = 0; i<10; i++) {
//
// a3[i] = arc4random() % (70-30+1) + 30;
// if (a3[i]< min) {
// min = a3[i];
//
// }
// }
//
// printf("min====%d \n",min) ;
for (int i=0; i<10-1; i++) {
for (int j=0; j<10-i-1; j++) {
a3[i] = arc4random() % (70-30+1) + 30;
if (a3[j]>a3[j+1]) {
int temp = a3[j];
a3[j]= a3[j+1];
a3[j+1] = temp;
max=a3[j+1]; printf("max===%d \n",max);
}
}
}
//4定義兩個整型數(shù)組,元素的個數(shù)為10,數(shù)組的取值范圍為隨機(jī)[20,60]將這兩個數(shù)組的元素放入一個新的數(shù)組中
// int a4[10],b4[10],c4[20];
// for (int i = 0; i<10; i++) {
// a4[i]=arc4random() % (60 - 20+1) +20;
// b4[i]=arc4random() % (60 - 20+1) +20;
//
// c4[i] = a4[i];
// c4[i+10] = b4[i];
// printf("a4=%d \n b4=%d",a4[i],b4[i]);
// }
// for (int i =0; i<20; i++) {
// printf("cccccc4=%d\n",c4[i]);
// }
//-------------------------------------------------------------------
//冒泡排序
//1.需要雙層循環(huán),外層控制循環(huán)的趟數(shù),內(nèi)層控制每趟循環(huán)的次數(shù)
//2.提高效率而言.外層趟數(shù)為元素個數(shù)-1,內(nèi)層循環(huán)次數(shù)處于遞減的狀態(tài),為元素個數(shù)-1(必須減,會越界)-i(為了提高效率,不和最后一個比較)
//3.VIP--內(nèi)存循環(huán)必須設(shè)置元素的個數(shù)-1的操作要防止下標(biāo)越界
// int f[6]={89,7,9,66,24,18};
// for (int i=0; i<6-1; i++) {
// for (int j = 0; j<6-1-i; j++) {
// //升序
// if (f[j]>f[j+1]) {
// //前者大于后者的話就交換位置
// int temp = f[j];
// f[j]= f[j+1];
// f[j+1] = temp;
//
// }
// }
// }
//
// for (int i = 0; i<6; i++) {
// printf("%d\n",f[i]);
// }