『壹』 c語言編程:字元串處理
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intsortArr(int*arr,intcount)
{
inti=0,j=0,tmp;
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(arr[i]>arr[j])
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
}
return0;
}
voidmain()
{
intrv=0;
intbuf[300]={0};
intcount=0;
inti=0;
chartmp;
intnum=0;
FILE*fp1=fopen("1.txt","r");
FILE*fp2=fopen("2.txt","r");
FILE*fp3=fopen("3.txt","w+");
if(fp1==NULL||fp2==NULL)
{
printf("文件打開失敗 ");
rv=-1;
gotoEnd;
}
while(!feof(fp1))
{
fscanf(fp1,"%d",&buf[count++]);
}
fclose(fp1);
while(!feof(fp2))
{
fscanf(fp2,"%d",&buf[count++]);
}
fclose(fp2);
sortArr(buf,count);
for(i=0;i<count;i++)
{
fprintf(fp3,"%d ",buf[i]);
}
fclose(fp3);
End:
system("pause");
}
『貳』 c語言編程 關於字元串的,用折半法,謝謝!
#include <stdio.h>
#include <string.h>
int find(char* str,char c, int start,int end) //折半查找
{
int i=start+(end-start)/2;
if(start>end) return -1;/* 沒找到,返回-1 */
if(str[i]==c) return i;
else if(str[i]>c) return find(str,c,i+1,end);
else return find(str,c,start,i-1);
}
void bubble_sort(char *a, int n) //冒泡排序
{
char *p1 = a;
char *p2 = a;
int i, j, k;
for (i = 0; i < n; i++)
{
p2 = p1;
p2++;
for (j = n - i - 1; j > 0; j--)
{
if (*p2 < *p1) // 升序
{
k = *p1;*p1 = *p2; *p2 = k;
}
p2++;
}
p1++;
}
}
void main() //主函數
{
char str[100];
char c;
printf("輸入字元串:");
scanf("%s",str);
printf("輸入字元:");
scanf("%c",&c);
bubble_sort(str, strlen(str));
if (find(str,c,0,strlen(str)-1)<0)
printf("該字元不在字元串中\n");
else
printf("該字元一直在字元串中\n");
}
『叄』 c語言編程(字元串)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//參數分別為開始位置,結束位置,源字元串,buffer
intCut(intstart,intend,char*source,char*buf)
{
inti=start,j=0;
intlen=strlen(source);
if(len<start)
{
return0;//開始的位置已經大於源字元串的最大長度,返回0
}
while(source[i]!='