格式化輸入輸出檔案
#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 />}