日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言字符串作业题,C语言课后习题练习(四)

發布時間:2023/12/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言字符串作业题,C语言课后习题练习(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(ps:提前聲明一下:課后習題是備份給自己看的😂)

1.輸入3個整數,按由小到大的順序輸出

int main() {

void swap(int *p1, int * p2);

int n1, n2, n3;

int *p1, *p2, *p3;

printf("input three integer n1,n2,n3:");

scanf("%d,%d,%d",&n1,&n2,&n3);

p1 = &n1;

p2 = &n2;

p3 = &n3;

if (n1 > n2)

swap(p1, p2);

if (n1 > n3)

swap(p1, p3);

if (n2 > n3)

swap(p2, p3);

printf("Now,the order is:%d,%d,%d\n",n1,n2,n3);

return 0;

}

void swap(int *p1, int *p2) {

int p;

p = *p1;

*p1 = *p2;

*p2 = p;

}

遠行結果:

c1.png

2.輸入三個字符串,按由小到大的順序輸出

//記得導入頭文件:#import

int main() {

void swap(char *, char *);

char str1[20],str2[31],str3[20];

printf("input three line:\n");

gets(str1);

gets(str2);

gets(str3);

if (strcmp(str1, str2) > 0)

swap(str1, str2);

if (strcmp(str1, str3) > 0)

swap(str1, str3);

if (strcmp(str2, str3) > 0)

swap(str2, str3);

printf("Now,the order is:\n");

printf("%s\n%s\n%s\n",str1,str2,str3);

return 0;

}

void swap(char *p1, char *p2){

char p[20];

strcpy(p, p1);

strcpy(p1, p2);

strcpy(p2, p);

}

運行結果:

c2.png

(注:在Xcode中運行才顯示的warning: this program uses gets(), which is unsafe.關于使用gets()不安全的警告)

3.寫一函數,求一個字符串的長度。在main函數中輸入字符串,并輸入其長度

int main() {

int length(char *p);

int len;

char str[20];

printf("input string: ");

scanf("%s",str);

len = length(str);

printf("The length of string is %d. \n",len);

return 0;

}

int length(char *p) { //求字符串長度函數

int n = 0;

while (*p != '\0') {

n++;

p++;

}

return(n);

}

運行結果:

c3.png

4.有一字符串,包含n個字符。寫一函數,將此字符串中從第m個字符開始的全部字符復制成為另一個字符串

//記得導入頭文件:#import

int main() {

void copystr(char *, char *, int);

int m;

char str1[20],str2[20];

printf("input string:\n");

gets(str1);

printf("which character that begin to copy?");

scanf("%d",&m);

if (strlen(str1) < m)

printf("input error!");

else {

copystr(str1, str2, m);

printf("result:%s\n",str2);

}

return 0;

}

void copystr(char *p1, char *p2, int m) { //字符串部分復制函數

int n = 0;

while (n < m - 1) {

n++;

p1++;

}

while (*p1 != '\0') {

*p2 = *p1;

p1++;

p2++;

}

*p2 = '\0';

}

運行結果:

c4.png

(注:在Xcode中運行才顯示的warning: this program uses gets(), which is unsafe.關于使用gets()不安全的警告)

5.輸入一行文字,找出其中大寫字母、小寫字母、空格、數字以及其他字符各有多少

//記得導入頭文件:#import

int main() {

int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;

char *p,s[20];

printf("input string: ");

while ((s[i] = getchar()) != '\n')

i++;

p = &s[0];

while (*p != '\n') {

if (('A' <= *p) && (*p <= 'Z'))

++upper;

else if (('a' <= *p) && (*p <= 'z'))

++lower;

else if (*p == ' ')

++space;

else if ((*p <= '9') && (*p >= '0'))

++digit;

else

++other;

p++;

}

printf("upper case:%d lower case:%d",upper,lower);

printf(" space:%d digit:%d other:%d\n",space,digit,other);

return 0;

}

運行結果:

c5.png

6.將n個數按輸入時順序的逆序排列,用函數實現

int main() {

void sort(char *p, int m);

int i, n;

char *p, num[20];

printf("input n:");

scanf("%d",&n);

printf("please input these numbers:\n");

for (i = 0; i < n; i++)

scanf("%d",&num[i]);

p = &num[0];

sort(p, n);

printf("Now,the sequence is:\n");

for (i = 0; i < n; i++)

printf("%d",num[i]);

printf("\n");

return 0;

}

void sort(char *p, int m) { //將n個逆序排列函數

int i;

char temp, *p1, *p2;

for (i = 0; i < m/2; i++) {

p1 = p + i;

p2 = p + (m - 1 - i);

temp = *p1;

*p1 = *p2;

*p2 = temp;

}

}

運行結果:

c6.png

總結

以上是生活随笔為你收集整理的c语言字符串作业题,C语言课后习题练习(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。