天天看點

C語言練習——Day1056、字元串排序。57、八進制轉換為十進制。58、填空練習(指向指針的指針)。59、計算字元串中子串出現的次數。60、時間函數

題目56-60

  • 56、字元串排序。
  • 57、八進制轉換為十進制。
  • 58、填空練習(指向指針的指針)。
  • 59、計算字元串中子串出現的次數。
  • 60、時間函數

56、字元串排序。

#include "stdio.h"
#include "string.h"

int main(){
    char a[20][20]={"china","korean","japan"};
    for (int i=0; i<3-1; i++) {
        for (int j=0; j<3-1-i; j++) {
            if (strcmp(a[j], a[j+1])>0) {
                char temp[20];
                strcpy(temp,a[j]);
                strcpy(a[j], a[j+1]);
                strcpy(a[j+1],temp);
            }
        }
    }
    for (int i=0; i<3; i++) {
        printf("%s\n",a[i]);
    }
}
           

利用指針:

#include "stdio.h"
#include "string.h"
int main(){
    char *str1[20],*str2[20],*str3[20];
    char swap();
    printf("please input three strings\n");
    scanf("%s",str1);
    scanf("%s",str2);
    scanf("%s",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("after being sorted\n");
    printf("%s\n%s\n%s\n",str1,str2,str3);
}
char swap(char *p1,char*p2){
    char *p[20];
    strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
           

運作結果:

C語言練習——Day1056、字元串排序。57、八進制轉換為十進制。58、填空練習(指向指針的指針)。59、計算字元串中子串出現的次數。60、時間函數

57、八進制轉換為十進制。

#include "stdio.h"
#include "string.h"

int main(){
    char *p,s[6];int n;
    gets(s);
    p=s;   
    n=0;
    while(*(p)!='\0'){
        n=n*8+*p-'0';
        p++;
    }
    printf("%d",n);
}
           

運作結果:

C語言練習——Day1056、字元串排序。57、八進制轉換為十進制。58、填空練習(指向指針的指針)。59、計算字元串中子串出現的次數。60、時間函數

58、填空練習(指向指針的指針)。

int main()
{
	char* s[] = { "man","woman","girl","boy","sister" };
	char** q;
	int k;
	for (k = 0;k < 5;k++)
	{
		q=&s[k];/*這裡填寫什麼語句*/
		printf("%s\n", *q);
	}
}
           

答案: q=&s[k];

運作結果:

C語言練習——Day1056、字元串排序。57、八進制轉換為十進制。58、填空練習(指向指針的指針)。59、計算字元串中子串出現的次數。60、時間函數

59、計算字元串中子串出現的次數。

程式分析:字元串c1,c2,c1逐字元與c2第一個字元比較,判斷是否相符,如果相同再比較其他字元直到c2中的字元全部比較完畢

#include<stdio.h>
int main()
{
	char c1[]="qwer123qwe45tyu";
    char c2[]="qwe";
    int i=0,number=0;
    while (c1[i]!='\0') {
        int j=0,flag=1;
        if (c1[i]==c2[j]) {
            while (c2[j]!='\0') {
                //printf("%c %c\n",c1[i],c2[j]);
                if (c1[i]==c2[j]) {
                    i++;j++;
                }
                else {
                    flag=0;
                    break;
                }
            }
            if (flag) {
                number++;
            }
        }
        i++;
        
    }
   printf("c1中含有%d個c2。",number);
}
           

利用指針:

#include<stdio.h>
int main()
{
	char c1[]="qwer123qwe45tyu";char c2[]="qwe";
    char *p1,*p2;p1=c1;
    int number=0;
    while (*p1!='\0') {
        p2=c2;
        int flag=1;
        if (*p1==*p2) {
            while (*p2!='\0') {
                if (*p1==*p2) {
                    p1++;p2++;
                }
                else {
                    flag=0;
                    break;
                }
            }
            if (flag) {
                number++;
            }
        }
        p1++;
        
    }
   printf("c1中含有%d個c2。",number);
}
           

運作結果:

C語言練習——Day1056、字元串排序。57、八進制轉換為十進制。58、填空練習(指向指針的指針)。59、計算字元串中子串出現的次數。60、時間函數

60、時間函數

#include <stdio.h>
#include <time.h>
 
int main ()
{
    time_t rawtime;
    struct tm * timeinfo;
    // 擷取系統目前時間
    time ( &rawtime );
    //localtime()是 把從1970-1-1零點零分到目前時間系統所偏移的秒數時間轉換為本地時間,傳回指針
    timeinfo = localtime ( &rawtime );
    // asctime函數把儲存的時間轉換為字元串
    printf ( "目前本地時間為: %s", asctime (timeinfo) );
    // gmtime函數轉換後的時間沒有經過時區變換,是UTC時間,中原標準時間比UTC時間快了8小時 
    printf("目前本地時間為: %s",asctime(gmtime(&rawtime))); 
    
    
    return 0;
}
           

運作結果:

C語言練習——Day1056、字元串排序。57、八進制轉換為十進制。58、填空練習(指向指針的指針)。59、計算字元串中子串出現的次數。60、時間函數

繼續閱讀