Common String Functions in ACM

Source: Internet
Author: User
Tags first string strfind strlen

1. String substitution

Syntax: replace (char Str[],char Key[],char swap[]);

Parameters:

Str[]:

Replace operation on this source string

Key[]:

The string to be replaced, cannot be an empty string

Swap[]:

The replacement string, which can be an empty string, is an empty string representing the deletion of key[in the source character]

return value:

Null

Attention:

Default str[] length less than 1000, if no, reset the TMP size setting

Need string.h

SOURCE program:

void replace (char Str[],char Key[],char swap[])
{
       int l1,l2,l3,i,j,flag;
       Char tmp[1000];
       L1=strlen (str);
       L2=strlen (key);
       L3=strlen (swap);

        for (i=0;i<=l1-l2;i++)
        {
             flag=1;
            for (j=0;j<l2;j++)
               if (Str[i+j]!=key[j]) 
              {
               flag=0;break;
               }
            if (flag)
            {
               strcpy (TMP,STR);
               strcpy (&tmp[i],swap);
               strcpy (&tmp[i+l3],&str[i+l2]);
               strcpy (str,tmp);
               i+=l3-1;
               L1=strlen (str);}}}


2. String Lookup

Syntax: Result=strfind (char Str[],char key[]);

Parameters:

Str[]:

Find operations on this source string

Key[]:

The string to be searched for cannot be empty

return value:

If the lookup succeeds, returns the position where key first appears in STR, otherwise returns-1

Attention:

Need string.h

SOURCE program:

int Strfind (char str[],char key[])
{
    int l1,l2,i,j,flag;
    L1=strlen (str);
    L2=strlen (key);
    for (i=0;i<=l1-l2;i++)
        {
        flag=1;
        for (j=0;j<l2;j++)
            if (Str[i+j]!=key[j]) {flag=0;break;}
        if (flag) return i;
        }
    return-1;
}

3. String interception

Syntax: Mid (char str[],int start,int Len,char strback[])

Parameters:

Str[]:

The target string for the operation

Start

Starts with the beginning of the first string and intercepts the Len-length characters

Len:

Starts with the beginning of the first string and intercepts the Len-length characters

Strback[]:

truncated to the character

return value:

0: Out of string length, intercept failed; 1: Intercept Successful

Attention:

Need string.h

SOURCE program:

int mid (char str[],int start,int len,char strback[])
{
    int l,i,k=0;
    L=strlen (str);
    if (start+len>l) return 0;
    for (i=start;i<start+len;i++)
        strback[k++]=str[i];
    strback[k]= ' + ';
    return 1;
}

4. String reversal function

Char *strrev (char string[])//string inversion function

    {
        char *start=string;
        char *left=string;
        char ch;
        while (*string + +);
        string-=2;
        while (left<string)
        {
                ch=*left;
                *left++=*string;
                *string--=ch;
        }
        return (start);
   

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.