Basic operation of String for C + + implementation of data structure

Source: Internet
Author: User
Tags bool truncated

A string is a finite sequence consisting of 0 or more characters. In general, the sequential storage structure is used, and the end ends with ' I ', but does not count to the length of the string.

Sample program: (Adapted from "liar data Structure")

#include <iostream>
using namespace Std;
#define MAXSIZE 20
typedef char STRING[MAXSIZE + 1]; End With ' a '
/* Generate a String * *
BOOL Strassign (String Dest, char *ptr)
{
cout << "Assign Str ..." << Endl;
int i;
for (i = 0; Ptr[i]!= ' && i < MAXSIZE; i++ '
Dest[i] = Ptr[i];
Dest[i] = ' the ';
return true;
}
/* Copy a String * *
BOOL Strcopy (String Dest, String Src)
{
cout << "Copy Str ..." << Endl;
int i;
for (i = 0; Src[i]!= ' && i < MAXSIZE; i++)
Dest[i] = Src[i];
Dest[i] = ' the ';
return true;
}

int Strlength (String Src)
{
int i = 0;
while (Src[i]!= ' ")
i++;
return i;
}

BOOL Strempty (String SRC)
{
if (strlength (SRC) = = 0)
return true;
Else
return false;

}
/* If STR1&GT;STR2, the return value >0; if STR1=STR2, the return value = 0; if str1<str2, the return value <0 * *
int Strcompare (string Str1, String Str2)
{
int len1 = Strlength (STR1);
int len2 = Strlength (STR2);
for (int i = 0; i < len1 && i < len2; i++)
if (Str1[i]!= str2[i])
return Str1[i]-str2[i];
return len1-len2;
}

BOOL Clearstring (String SRC)
{
for (int i = 0; Src[i] "!="; i++)
Src[i] = ' the ';
return true;
}
/* Use Dest to return a new string of STR1 and STR2 joins. Returns true if not truncated, or false.
BOOL Strconcate (String Dest, String Str1, String Str2)
{
cout << "concate String ..." << Endl;
if (Strlength (STR1) + strlength (STR2) <= MAXSIZE)
{
/* Not truncated */
int I, J;
for (i = 0; Str1[i] "!="; i++)
Dest[i] = Str1[i];
j = i;
for (i = 0; Str2[i] "!="; i++, J + +)
DEST[J] = Str2[i];
DEST[J] = ' the ';
return true;
}
else//truncated STR2
{
int I, J;
for (i = 0; Str1[i] "!="; i++)
Dest[i] = Str1[i];
j = i;
for (i = 0; Str2[i]!= ' && J <= MAXSIZE-1; i++, J + +)
DEST[J] = Str2[i];
DEST[J] = ' the ';
return false;
}

}
/* Use sub to return the first POS character of string src with a substring of Len. */
BOOL SubString (String Sub, string Src, int pos, int len)
{
/*cout<< "Get SubString ..." <<endl;*/
if (pos < 1 | | | pos > Strlength (SRC) | |
Len < 0 | | Len > Strlength (SRC)-pos + 1)
return false;
int i;
for (i = 0; I <= len-1; i++)
Sub[i] = src[i + pos-1];
Sub[i] = ' the ';
return true;
}
/* Returns the position of the sub-string sub in the main string src after the pos character. If it does not exist, the function returns a value of 0. */
int Index1 (string Src, string Sub, int pos)
{
int len1 = Strlength (SRC);
int len2 = Strlength (Sub);
int i = pos;
String Sub;
if (pos > 0)
{
while (i <= len1-len2 + 1)
{
SubString (Sub, SRC, I, len2);
/* If two strings are equal, return the I value * *
if (Strcompare (sub, sub) = = 0)
return i;
else/* if two strings are not equal, forward position * *
i++;
}
}
return 0;
}

int Index2 (string Src, string Sub, int pos)
{
int i = pos-1;
int j = 0;
int len1 = Strlength (SRC);
int len2 = Strlength (Sub);
while (I <= len1-1 && J <= len2-1)
{
if (src[i] = = Sub[j])/* Two letter is equal to continue * *
{
++i;
++j;
}
Else
{
/* I back to the last match first of the next * *
i = i-j + 1;
j = 0;/* J returns to the first of sub string sub
}
}

if (j = = len2)//substring has been traversed
return i-len2 + 1;
Else
return 0;
}
/* Insert string in before the ordinal pos character of string src. Full Insert returns True, partial insert returns false * *
BOOL Strinsert (string Src, int pos, String in)
{
int i;
int len1 = Strlength (SRC);
int len2 = Strlength (in);
if (pos < 1 | | | pos > LEN1 + 1)
return false;

if (len1 + len2 <= MAXSIZE)
{
/* Full Insert/*
for (i = len1 i >= pos-1; i--)
Src[i + len2] = Src[i];
for (i = Pos-1 i < pos + len2-1; i++)
Src[i] = In[i-pos + 1];
if (pos = = len1 + 1)//end INSERT, finally add ' the '
Src[i] = ' the ';
return true;
}

Else
{
/* Part INSERT, in truncation * *
for (i = MAXSIZE i > pos; i--)
Src[i] = Src[pos + i-maxsize];

for (i = 0; i < Maxsize-pos; i++)
Src[pos-1 + i] = in[i];
return false;
}
}
/* Remove the first pos character from string src (the substring of Len)
BOOL Strdelete (String Src, int pos, int len)
{
int i;
if (pos < 1 | | | pos > Strlength (SRC)-len + 1 | | Len < 0)
return false;
for (i = pos + len-1 i <= strlength (SRC); i++)
Src[i-len] = Src[i];
return true;
}
/* Replace all the non-overlapping substrings that appear in the main string src with the sub/* with the RE
BOOL Strreplace (String Src, String Sub, String Re)
{
int i = 1;/* the first character of string src to find a string sub */
if (Strempty (Sub))
return false;
Todo
{
i = Index1 (SRC, Sub, i);/* result I is the position of the substring found after last I

if (i)
{
Strdelete (SRC, I, Strlength (Sub));/* Delete the string Sub */
Strinsert (SRC, I, Re); /* Insert the string re in the position of the original string sub * *
i + + strlength (re);/* Continue to find the string sub after inserting the string Re */
}

}
while (i);

return true;
}

void Strprint (String Src)
{
cout << "Print Str ..." << Endl;
for (int i = 0; Src[i] "!="; i++)
cout << Src[i];
cout << Endl;

}

int main (void)
{
String Str1;
Strassign (STR1, "ILOVEYOU");
Strprint (STR1);

String Str2;
Strcopy (STR2, STR1);
Strprint (STR2);

if (! Strempty (STR1))
cout << "Str1 ' s Length:" << strlength (STR1) << Endl;

String STR3;
Strassign (STR3, "ILOVEyou");

if (Strcompare (STR1, STR3) > 0)
cout << "Str1 > Str3" << endl;
else if (Strcompare (STR1, Str3) = = 0)
cout << "Str1 = STR3" << Endl;
Else
cout << "Str1 < STR3" << Endl;

String STR4, STR5;
Strassign (STR4, "Hlz");
Strconcate (STR5, STR1, STR4);
Strprint (STR5);

String STR6;
cout << "Get SubString ..." << Endl;
SubString (STR6, STR5, 1, 8);
Strprint (STR6);

cout << "Index of (STR5, STR4)" << Index2 (STR5, STR4, 2) << Endl;

Strinsert (STR6, 9, "Hlz");
Strprint (STR6);
Strinsert (STR6, 8, "Hlz");
Strprint (STR6);

Strdelete (STR5, 2, 4);
Strprint (STR5);

String STR7, Str8;
Strassign (STR7, "Ilovejdwsovedsovede");
Strassign (Str8, "OVE");
Strreplace (STR7, Str8, "Ove");
Strprint (STR7);

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.