C&C++常用小函數 代碼

來源:互聯網
上載者:User

格式化輸入輸出檔案

#include "stdafx.h"<br />#include <stdlib.h><br />#include "string.h"<br />#include "stdio.h"<br />int main()<br />{<br /> FILE *fp;<br /> int number[3];<br /> int score[3];<br /> int i = 0;<br /> int rece_Number = 0;<br /> if((fp = fopen("test.txt","r")) == NULL)<br /> {<br /> fprintf(stderr,"Error opening file!/n");<br /> }<br /> for(i =0;i<3;i++)<br /> {<br /> fscanf(fp,"%d %d",&number[i],&score[i]);//學號與成績用空格<br /> }<br /> printf("Please input number:/n");<br /> scanf("%d",&rece_Number);<br /> for(i = 0;i<3;i++)<br /> {<br /> if(rece_Number == number[i])<br /> break;<br /> }<br /> printf("The Number's score is %d/n",score[i]);<br /> fclose(fp);<br /> return 0;<br />}<br /> 

統計字串中最長的重複字串

#include "stdafx.h"<br />#include "string.h"<br />#include "malloc.h"</p><p>int main()<br />{<br /> int i,j,k,t;<br /> int max = 0;<br /> char *p = NULL;<br /> char str[42] = "2xtuaojy5678zuaojy908puaojy417uaojy356";<br /> char temp[8];<br /> char result[8];<br /> memset(result,0,8);<br /> p = str;<br /> for(i=0;i <42;i++)<br /> {<br /> for(j=i+1;j <42;j++)<br /> {<br /> if(*(p+i)== *(p+j))<br /> {<br /> t = i;<br /> k = 0;<br /> memset(temp,0,8);<br /> //只要找到了相同的首字元,就開始比較直到沒有相同的字元,記錄當前字串,看是否是最長的<br /> while(*(p+t)== *(p+j))<br /> {<br /> temp[k] = *(p+t);//不斷比較後續的字元<br /> t++;<br /> j++;<br /> k++;<br /> }<br /> if(k > max)//將最長的字串賦值給result<br /> {<br /> max = strlen(temp);<br /> strcpy(result,temp);<br /> }</p><p> } </p><p> }</p><p> }<br /> //列印出結果在上述字串中最長的是 "zuaojy"<br /> printf("重複字串最長的是 %s,長度為 %d /n",result,max);<br /> return 0;<br />} 

 

通過遞迴求字串中出現的相同字元次數

int count_times(char ch,const char* str)<br />{<br /> int count = 0;<br /> if(*str == '/0')<br /> {<br /> count = 0;<br /> }<br /> else<br /> {<br /> if((*str)==ch)<br /> {<br /> count = 1+count_times(ch,str+1);<br /> }<br /> else<br /> {<br /> count = count_times(ch,str+1);<br /> }<br /> }<br /> return count;<br />} 

聯繫我們

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