您的位置:首页 > 其它

字符串高级处理函数(Strcmp,Substr,Strstr,partition)

2007-10-18 21:59 375 查看
Strcmp是针对两字符串的每个字符两两作比较(区分大小写),直到两字符串中有字符不相等或字符串已结束。若两字符串相等,返回0;若两字符串不相等,则用ASCII码来比较不同的字符,以决定字符串的大小。

Substr截取原始字符串(可能是由多个字符串所组成)中一个或一个以上连续字符所组成的字符串,这些截取出来的字符串都是原始字符串的子字符串。

Strstr是对原始字符串进行字符的比较,以判断某特定字符串是否存在于该原始字符串中,若存在则此特定的字符串为原始字符串的子字符串。

partition是以空格为分割点对字符串(假定子字符串间与子字符串间用空格分开)进行分割,在进行分割时,不管有几个连续空格均会被忽略掉,即可得到分割的结果。

#include <stdio.h>
#include <string.h>

int Strcmp(char *s1,char *s2)//字符串的比较
{
int i;
for (i=0;s1[i]==s2[i];i++)
{
if (s1[i]=='/0' && s2[i]=='/0')//字符串s1等于字符串s2
{
return 0;//s1=s2返回值为0
}
}
if (s1[i]>s2[i])//字符串s1大于字符串s2
{
return 1;//s1>s2返回值为1
}
return -1;//s1<s2返回值为-1
}

char *Substr(char *s,int pos,int len,char *sub)//字符串的截取
{
// char s1[50];//错误!如果在此声明子字符串数组,则为局部变量,函数调用完后即刻释放,得不到返回值
int i,j,endpos;
pos--;//开始的位置
endpos=pos+len-1;//结束的位置
for (i=pos,j=0;i<=endpos;i++,j++)
{
sub[j]=s[i];//sub为声明的子字符串数组(形参),作为函数返回值
}
sub[len]='/0';//设置子字符串的结束字符
return sub;
}

int Strstr(char *s1,char *s2)//字符串的查找
{
int i,j,endpositoion;
endpositoion=strlen(s1)-strlen(s2);//结束比较的位置
if (endpositoion>0)//若子字符串小于字符串长度才进行比较
{
for (i=0;i<endpositoion;i++)
{
for (j=i;s1[j]==s2[j-i];j++)//比较各字符是否相等
{
if (s2[j-i+1]=='/0')//子字符串结束
{
return i+1;//返回子字符串出现的位置(从1计数)
}
}
}
}
return -1;
}

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 s1[50];
char s2[50];
char string[100];
char substring[100];//存储所截取的子字符串
char partition_string[20];//声明分割字符串数组
char temp[5];
int compare;//存储比较结果的变量
int position;//截取起始位置
int cut_position;//分割的位置
int length;//截取的子字符串长度
int final_result;//查找的最终结果
int k;

printf("/nPlease input string1:");
gets(s1);
printf("/nPlease input string2:");
gets(s2);
compare=Strcmp(s1,s2);//字符串大小的比较
printf("/nstring1:%s",s1);
printf("/nstring2:%s",s2);
printf("/ncompare result:");
switch(compare)
{
case 0:
printf("string1=string2/n");
break;
case 1:
printf("string1>string2/n");
break;
case -1:
printf("string1<string2/n");
break;
}

printf("/nPlease input string:");
gets(string);
printf("/nPlease input start position:");
scanf("%d",&position);
printf("/nPlease input substring length:");
scanf("%d",&length);
gets(temp);//吃掉上一行末的回车
Substr(string,position,length,substring);//进行字符串截取
printf("/nThe substring is: '%s'/n",substring);

printf("/nPlease input the string:");
gets(string);
printf("/nPlease input the finding substring:");
gets(substring);
final_result=Strstr(string,substring);//进行字符串的查找
if (final_result>0)
{
printf("The start position of substring '%s' is [%d]/n",substring,final_result);
}
else
printf("The substring '%s' is not in the string/n",substring);

printf("/nPlease input string:");
gets(string);
cut_position=0;//设置进行分割的第一个位置
printf("/nPartition result:/n");
/*循环调用分割函数进行字符串分割,直到字符串结束*/
k=0;
while ((cut_position=partition(string,partition_string,cut_position))!=-1)
{
k++;
printf("Partition %d :%s/n",k,partition_string);//输出各分割出来的子字符串
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐