//-----------1字符串的复制-------------
#include"stdio.h"
char *strcpy(char *s1,char *s2){
int i;
for(i=0;s2[i]!='\0';i++)
s1[i]=s2[i];
s1[i]='\0';
return s1;
}
void main(){
char string[50];
char copystring[50];
printf("\nPlease input string:");
gets(string);
strcpy(copystring,string);
printf("\nString:%s",string);
printf("\nCopystring:%s",copystring);
}
//-----------2字符串的替换-------------
#include"stdio.h"
char *strrep(char *s1,char *s2,int pos){
int i,j;
pos--;
i=0;
for(j=pos;s1[j]!='\0';j++)
if(s2[i]!='\0') {
s1[j]=s2[i];
i++;
}else
break;
return s1;
}
void main(){
char string1[100];
char string2[50];
int position;
printf("\nPlease input original string:");
gets(string1);
printf("\nPlease input substitute string:");
gets(string2);
printf("\nPlease input substitute position:");
scanf("%d",&position);
strrep(string1,string2,position);
printf("\nThe final string:%s\n",string1);
}
//-----------3字符串的删除-------------
#include"stdio.h"
char *strdel(char *s,int pos,int len){
int i;
pos--;
for(i=pos+len;s[i]!='\0';i++)
s[i-len]=s[i];
s[i-len]='\0';
return s;
}
void main(){
char string[50];
int position;
int length;
printf("\nPlease input original string:");
gets(string);
printf("\nPlease input delete position:");
scanf("%d",&position);
printf("\nPlease input delete length:");
scanf("%d",&length);
strdel(string,position,length);
printf("\nThe final string:%s",string);
}
//-----------4字符串的比较-------------
#include"stdio.h"
char *strcmp(char *s1,char *s2){
int i,j;
for(i=0;s1[i]==s2[i];i++)
if(s1[i]=='\0'&&s2[i]=='\0')
return 0;
if(s1[i]>s2[i])
return 1;
return -1;
}
void main(){
char s1[50];
char s2[50];
int compare;
printf("\nPlease input string(1):");
gets(s1);
printf("\nPlease input string(2):");
gets(s2);
compare=strcmp(s1,s2);
printf("\nString(1):%s",s1);
printf("\nString(2):%s",s2);
printf("\nCompare result:");
switch(compare){
case 0:
printf("\nString(1)=String(2)\n");
break;
case 1:
printf("\nString(1)>String(2)\n");
break;
case -1:
printf("\nString(1)<String(2)\n");
break;
}
}
//-----------5字符串的抽取-------------
#include"stdio.h"
char *substr(char *s,int pos,int len){
char s1[50];
int i,j,endpos;
pos--;
endpos=pos+len-1;
for(i=pos,j=0;i<=endpos;i++,j++)
s1[j]=s[i];
s1[len]='\0';
printf("\nThe substring is '%s'\n",s1);
return s1;
}
void main(){
char string[100];
char *substring;
int position;
int length;
printf("\nPlease input string:");
gets(string);
printf("Please input start position:");
scanf("%d",&position);
printf("Please input substring length:");
scanf("%d",&length);
substring=substr(string,position,length);
}
//-----------6字符串的分割-------------
#include"stdio.h"
int partition(char *s1,char *s2,int pos){
int i,j;
i=pos;
while(s1[i]==' ')
i++;
if(s1[i]!='\0'){
j=0;
while(s1[i]!='\0'&&s1[i]!=' '){
s2[j]=s1[i];
i++;
j++;
}
s2[j]='\0';
return i;
}else
return -1;
}
void main(){
char string[50];
char partition_string[20];
int position;
int k;
printf("\nPlease input strng:");
gets(string);
position=0;
printf("\nPartition result:\n");
k=0;
while((position=partition(string,partition_string,position))!=-1){
k++;
printf("Partition %d:%s\n",k,partition_string);
}
}