字串進階處理函數(Strcmp,Substr,Strstr,partition)__函數

來源:互聯網
上載者:User

 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);//輸出各分割出來的子字串
 }
}

 

聯繫我們

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