C語言中的字串截取函數及應用

來源:互聯網
上載者:User


/*========================================================
子數整數
來源程式名 num.??? (pas,c,cpp)
可執行檔名 num.exe
輸入檔案名稱 num.in
輸出檔案名 num.out
對於一個五位元a1a2a3a4a5,可將其拆分為三個子數:
sub1=a1a2a3
sub2=a2a3a4
sub3=a3a4a5
例如,五位元20207可以拆分成
sub1=202
sub2=020(=20)
sub3=207
現在給定一個正整數K,要求你編程求出10000到30000之間所有滿足下述條件的五位元,
條件是這些五位元的三個子數sub1,sub2,sub3都可被K整除。
輸入
輸入由鍵盤輸入,輸入僅一行,為正整數K(0<K<1000)。
輸出
輸出到檔案,輸出檔案的每一行為一個滿足條件的五位元,要求從小到大輸出。
不得重複輸出或遺漏。如果無解,則輸出“No”。

範例
num.in
15
num.out
22555
25555
28555
30000

==========================================================*/

#include <stdio.h>
#include <string.h>

/*從字串的左邊截取n個字元*/
char * left(char *dst,char *src, int n)
{
    char *p = src;
    char *q = dst;
    int len = strlen(src);
    if(n>len) n = len;
    /*p += (len-n);*/   /*從右邊第n個字元開始*/
    while(n--) *(q++) = *(p++);
    *(q++)='\0'; /*有必要嗎?很有必要*/
    return dst;
}

/*從字串的中間截取n個字元*/
char * mid(char *dst,char *src, int n,int m) /*n為長度,m為位置*/
{
    char *p = src;
    char *q = dst;
    int len = strlen(src);
    if(n>len) n = len-m;    /*從第m個到最後*/
    if(m<0) m=0;    /*從第一個開始*/
    if(m>len) return NULL;
    p += m;
    while(n--) *(q++) = *(p++);
    *(q++)='\0'; /*有必要嗎?很有必要*/
    return dst;
}

/*從字串的右邊截取n個字元*/
char * right(char *dst,char *src, int n)
{
    char *p = src;
    char *q = dst;
    int len = strlen(src);
    if(n>len) n = len;
    p += (len-n);   /*從右邊第n個字元開始*/
    while(*(q++) = *(p++));
    return dst;
}

void main()
{
    FILE * p;
    int i,k,outi,count=0;
    int sub1,sub2,sub3;
    char *strsub1,*strsub2,*strsub3,*strtempnum,*a,*b,*c;

    if((p = fopen("num.out", "ab+")) == NULL)
    {
        printf("open file fail!");
        getch();
        exit();
    }
    printf("Please input int number(0<K<1000):");
    scanf("%d",&k);

    for(outi=10000;outi<=30000;outi++)
    {
        itoa(outi,strtempnum,10);

        left(strsub1,strtempnum,3);
        mid(strsub2,strtempnum,3,1);
        right(strsub3,strtempnum,3);

        /*
        a=strsub1;
        b=strsub2;
        c=strsub3;
        printf("strsub1=%s,strsub2=%s,strsub3=%s\n",a,b,c);
        */

        sub1=atoi(strsub1);
        sub2=atoi(strsub2);
        sub3=atoi(strsub3);

        /*
        printf("sub1=%d , sub2=%d , sub3=%d \n\n",sub1,sub2,sub3);
        printf("sub1k=%d , sub2k=%d , sub3k=%d \n\n" , sub1 % k,sub2 % k,sub3 % k);
        getch();
        */
        if((sub1%k)==0 && (sub2%k)==0 && (sub3%k)==0)
        {
            fprintf(p,"%d\n",outi);
            count++;
            printf("outi=%d\n",outi);
        }
        else
        {
            fprintf(p,"%s\n","NO");
        }
    }
    printf("Count=%d    OK",count);
    fclose(p);
    getch();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.