A string is defined as the finite sequence of the characters(elements) in alphabet \(\Sigma\). The number of characters is the length of string, specially, if there’s no character in a string, or the length of string is 0, it’s an empty string. Any subsequence of the string sequence is a substring.
Based on the definition, a string is no more than a linear list of characters, which implies that any storage stragey and algorithm designed for linear list can be used on string.
String is supported as an indivual data type by most programming language. While for C language core, there’re no bulit-in string type support, instead, string is represented by the char
array, which contains a \0
(ASCII 0
) as the terminal singal(character). Further details are illustrated in the C Programming Language’s Notes String. We’ll use the some codes like following to implement a string:
#define MAXN 100;
char str[MAXN];
Useful function of string usually include two types
strlen(str)
.strsub(str, start, end)
.delete(str, del_start_at, del_length)
.strcmp(str1, str2)
.insert(str, pos, inserted_string)
. This can be degenerated as char insertion and string/char append.strstr(str, pattern)
. Normally the base algorithm is only required to find the first occurance since the findall
case is simply the repeat. Combined with the position-range-specific deletion introduced before, we can delete the given patterns in strings.We’ll introduce and implement all of these operations manually except pattern reconigition. The algorithms which implement that task would be illustrate in the indivual section.
/* Common Codes */
#define FAIL 0;
#define SUCCESS 1;
char s[MAXN], s1[MAXN], s2[MAXN];
Get length of string:
int strlen(char str[]){
int i;
for(int = 0; str[i]!='\0'; i++);
return i;
}
Range based deletion:
int delete(char s[], del_start_at, del_length){
int raw_length = strlen(s);
if(del_start_at >= raw_length) return SUCCESS;
int seg_start_at = del_start_at + length;
for(int i=seg_start_at; i<raw_length; i++){
str[del_start_at + i] = str[seg_start_at + i];
} }