Data structure Practice-sequential string algorithm

Source: Internet
Author: User

This article is aimed at the data structure Basic Series Network course (4): String Practice Project.

"Project-Sequential string algorithm"
Use sequential storage to store strings, implement the following algorithms and test:
(1) Try to write the algorithm to replace all characters in the string s with values of C1 into characters with a value of C2:
void Trans (sqstring *&s, Char C1, char C2);
(2) Try to write an algorithm that implements all characters of a known string to be rearranged backwards. If the abcdef is changed to FEDCBA.
void Invert (sqstring &s)
(3) Remove all characters whose value equals C from string S. If you remove ' e ' from the message, you get MSSAG.
void Dellchar (sqstring &s, char c)
(4) There are two strings S1 and S2, design an algorithm to find a such string, the characters in the string are S1 and S2 common characters. The so-called common substring is a character that is composed of characters in the S1, and also in S2. Example S1 is "message" and S2 is "agent", the common substring obtained is "eage".
Sqstring Commchar (sqstring s1,sqstring S2);

[Refer to answer] (header file sqstring.h see sequential string algorithm library)
(1) Try to write the algorithm to replace all characters in the string s with values of C1 into characters with a value of C2:
void Trans (sqstring *&s, Char C1, char C2);
Reference:

#include <stdio.h>
#include "sqString.h"
void Trans (sqstring &s, Char C1, char C2)
{
    int i;< C4/>for (i=0; i<s.length; i++)
        if (S.DATA[I]==C1)
            s.data[i]=c2;
}

int main ()
{
    sqstring s;
    Strassign (S, "messages");
    Trans (S, ' e ', ' a ');
    DISPSTR (s);
    return 0;
}

(2) Try to write an algorithm that implements all characters of a known string to be rearranged backwards. If the abcdef is changed to FEDCBA.
void Invert (sqstring &s)

Reference:
The first element in the string is swapped with the last element, the second element is exchanged with the penultimate element, and so on, all characters are exchanged, and the string is reversed.

#include <stdio.h>
#include "sqString.h"
void Invert (sqstring &s)
{
    int i;
    char temp;
    for (i=0; i<s.length/2; i++)
    {
        temp = s.data[i];
        S.DATA[I]=S.DATA[S.LENGTH-I-1];
        S.DATA[S.LENGTH-I-1] = temp;
    }
}

int main ()
{
    sqstring s;
    Strassign (S, "ABCDEFG");
    Invert (s);
    DISPSTR (s);
    return 0;
}

(3) Remove all characters whose value equals C from string S. If you remove ' e ' from the message, you get MSSAG.
void Dellchar (sqstring &s, char c)

Reference: Scan the S string from start to finish, and delete the element whose value is C by moving it.

#include <stdio.h>
#include "sqString.h"
void Dellchar (sqstring &s, char c)
{
    int k=0, i=0;< The c4/>//k record value equals the number of characters in C
    while (I<s.length)
    {
        if (s.data[i]==c)
            k++;
        else
            S.data[i-k]=s.data[i];
        i++;
    }
    S.length-= k;
}

int main ()
{
    sqstring s;
    Strassign (S, "message");
    Dellchar (S, ' e ');
    DISPSTR (s);
    return 0;
}

(4) There are two strings S1 and S2, design an algorithm to find a such string, the characters in the string are S1 and S2 common characters. The so-called common substring is a character that is composed of characters in the S1, and also in S2. Example S1 is "message" and S2 is "agent", the common substring obtained is "eage".
Sqstring Commchar (sqstring s1,sqstring S2);

Reference: For each character in S1, see if it appears in S2, and if so, add it to the result string.

 #include <stdio.h> #include "sqString.h" sqstring commchar (sqstring s1,sqstring s2) {
    Sqstring S3;
    int i,j,k=0;
                For (i=0, i<s1.length; i++) {for (j=0; j<s2.length; j + +) if (S2.data[j]==s1.data[i])
        Break
            if (j<s2.length)//s1.data[i] is the public character {s3.data[k]=s1.data[i];
        k++;
    }} s3.length=k;
return S3;
    } int main () {sqstring S1, S2, S;
    Strassign (S1, "message");
    Strassign (S2, "agent");
    s = Commchar (S1, S2);
    DISPSTR (s);
return 0; }

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.