C語言課后習(xí)題練習(xí)(四)

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

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

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;
}

遠(yuǎn)行結(jié)果:
c1.png

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

//記得導(dǎo)入頭文件:#import <string.h>
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);
}

運行結(jié)果:
c2.png

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

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

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) {          //求字符串長度函數(shù)
    int n = 0;
    while (*p != '\0') {
        n++;
        p++;
    }
    return(n);
}

運行結(jié)果:
c3.png

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

//記得導(dǎo)入頭文件:#import <string.h>
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) {     //字符串部分復(fù)制函數(shù)
    int n = 0;
    while (n < m - 1) {
        n++;
        p1++;
    }
    while (*p1 != '\0') {
        *p2 = *p1;
        p1++;
        p2++;
    }
    *p2 = '\0';
}

運行結(jié)果:
c4.png

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

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

//記得導(dǎo)入頭文件:#import <string.h>
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;
}

運行結(jié)果:
c5.png

6.將n個數(shù)按輸入時順序的逆序排列,用函數(shù)實現(xià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個逆序排列函數(shù)
    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;
    }
}

運行結(jié)果:
c6.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容