String advanced processing function (strcmp,substr,strstr,partition) __ function

Source: Internet
Author: User

STRCMP is a comparison of each character 22 of two strings (case-sensitive) until the characters are unequal or the string is ended in two strings. If the two strings are equal, return 0; If the two strings are not equal, then use the ASCII code to compare different characters to determine the size of the string.

Substr intercepts a string of one or more contiguous characters in the original string, which may be composed of multiple strings, which are substrings of the original string.

Strstr is a character comparison of the original string to determine whether a particular string exists in the original string, and if so, this particular string is a substring of the original string.

Partition is a space for the split point on the string (assuming that the substring between the substring separated by a space), in the segmentation, no matter how many consecutive spaces will be ignored, you can get the result of segmentation.

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

Comparison of int Strcmp (char *s1,char *s2)//String
{
int i;
for (i=0;s1[i]==s2[i];i++)
{
if (s1[i]== '/0 ' && s2[i]== '/0 ')//string S1 equals string s2
{
Return 0;//S1=S2 returns a value of 0
}
}
if (S1[i]>s2[i])//string S1 greater than string s2
{
Return 1;//S1>S2 Returns a value of 1
}
RETURN-1;//S1<S2 return value is-1
}

Char *substr (char *s,int pos,int len,char *sub)//String interception
{
// char s1[50];//error. If you declare a substring array here, the local variable is immediately released after the function call, and the return value,
 int i,j,endpos;
Where the  pos--;//starts
 endpos=pos+len-1;//end
 for (i=pos,j=0;i<=endpos;i++,j++)
 {
  sub[j]=s[i];//sub is a declared substring array (formal parameter), as function return value
 
 sub[len]= '/0 ';//set end character of substring
 return Sub;
}

Int strstr (char *s1,char *s2)//String lookup
{
 int i,j,endpositoion;
 endpositoion=strlen (S1)-strlen (S2);//End comparison position
 if (endpositoion>0)//Jozo string is less than string length to compare
  {
  for (i=0;i<endpositoion;i++)
  {
   for (j=i;s1[j]==s2[j-i];j + +)//Compare the characters for equality
   {
    if (s2[j-i+1]== '/0 ')/substring end
     {
     return i+1;//returns where the substring appears (count from 1)
    
&NBSP;&NBSP;&NBSP}
  }
 
 return-1;
}

Int partition (char *s1,char *s2,int POS)/String split, calling a function can only split one substring
{
 int i,j;
 i=pos;//from the position to split the
 while (s1[i]== ')/Ignore all spaces before the string
 {
  i++;
&NBSP}
 if (s1[i]!= '/0 ')/To determine whether the string is closed
 {
  j=0;
  while (s1[i]!= '/0 ' && s1[i]!= ')/copy non-spaces until you find the next spaces
  {
   s2[j ]=s1[i];
   i++;
   j++;
  }
  s2[j]= '/0 ';//sets the end character of the split string
  return i;//returns the current position
 
 else
   return-1;//end Split
}

Void Main ()
{
 char s1[50];
 char S2[50];
 char string[100];
 char substring[100];//Store intercepted substring
 char partition_string[20];//declares a split string array
 char temp[5];
 int compare;//The variable that stores the comparison result
 int position;//intercept the start position
 int cut_position;//the split position
 int length ;//intercepted substring length
 int final_result;//The final result of the lookup
 int K;

 printf ("/nplease input string1:");
 gets (S1);
 printf ("/nplease input string2:");
 gets (S2);
 compare=strcmp (S1,S2);//String size comparison
 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);/to eat the carriage return at the end of the line
Substr (string,position,length,substring);//For string interception
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);//Searching for strings
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;//setting is the first place to split
printf ("/npartition result:/n");
/* Loop call Split function for string segmentation until end of string
k=0;
while ((Cut_position=partition (string,partition_string,cut_position))!=-1)
{
k++;
printf ("Partition%d:%s/n", k,partition_string);//output separated substring
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.