字串排序-大寫後排程式

來源:互聯網
上載者:User
這是,前面是個原字串,隨機產生,後面是排序後的字串,將大寫字母後排,保證相對位置不變。以為是代碼:
#include"test_char_exchange.h"#include<stdlib.h>#include<stdio.h>int isup(char c){return c>='A'&&c<='Z'?1:0;}void swap(char *c1,char *c2){int t=*c1;*c1=*c2;*c2=t;}int exchange(char*cs,int f,int r)//返回大寫字母個數{if(f==r)return isup(cs[f]);int m=(f+r)/2;int k1=exchange(cs,f,m);//前半部分排序int k2=exchange(cs,m+1,r);//後半部分排序if(k1>0&&k2<r-m)//如果前半部分有大寫字串或者後半部分有小寫字串,則兩者交換位置{int b=m-k1+1;//交換起始點int e=r-k2;//交換終點while(e-m!=m-b+1)//如果需要交換的兩字串長度不相等,則迴圈交換使相等{while(e-m>m-b+1&&m>=b)//如果後半部分字串更長{int temp=m;//記下前半部分字串的終點while(m>=b)swap(&cs[m--],&cs[e--]);//將cs[b:m]部分交換到cs[……:e]m=temp;//恢複前半部分終點}while(e-m<m-b+1&&m>=b)//如果後半部分更短swap(&cs[m--],&cs[e--]);//將後半部分字串交換到前半部分的末端}while(m>=b)swap(&cs[m--],&cs[e--]);//兩字串長度相等,可以依次交換}return k1+k2;}#define MLEN 1024int test(){char cs[MLEN+1]="";for(int k=0;k<1;k++){for(int j=0;j<MLEN;j++){int m=rand()%100;if(m>50)cs[j]=m%25+'a';elsecs[j]=m%25+'A';}printf("\n %s \n",cs);exchange(cs,0,MLEN-1);int p=0,q=MLEN-1;while((!isup(cs[p]))&&p<MLEN)p++;while(isup(cs[q])&&q>=0)q--;if(p-q!=1)return 0;printf("\n %s \n",cs);}return 1;}

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.