String manipulation is the action of the fundamental
operations on strings, including their creation, concatenation, the extraction
of string segments, string matching, their comparison, discovering their
length, replacing sub-strings by other strings. Here we demonstrate some example
of string manipulation.
Source code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
/* string length */
int StrLength(char
*string)
{
char *count = string;
while (*count)
{
count++;
}
return count - string;
}
/* string append */
char* StrAppend(char*
string, char* append)
{
char* newstring = NULL;
size_t needed = _snprintf(NULL, 0, "%s%s", string, append);
newstring
= (char*)(malloc(needed));
sprintf(newstring,
"%s%s", string, append);
return newstring;
}
/* string append char */
char* StrAppendChar(char*
string, char append)
{
char* newstring = NULL;
size_t needed = _snprintf(NULL, 0, "%s%c", string, append);
newstring
= (char*)(malloc(needed));
sprintf(newstring,
"%s%c", string, append);
return newstring;
}
/* string equals */
int StrEquals(char
*equal1, char *eqaul2)
{
while (*equal1 == *eqaul2)
{
if (*equal1 == `\0` || *eqaul2 == `\0`)
{
break;
}
equal1++;
eqaul2++;
}
if (*equal1 == `\0` && *eqaul2 == `\0`)
{
return 0;
}
else
{
return -1;
}
}
/* string replace */
char* StrReplace(char*
search, char* replace, char* subject)
{
char* newstring = "";
int i = 0;
for (i = 0; i < StrLength(subject); i++)
{
if (subject[i] == search[0])
{
int e = 0;
char* calc = "";
for (e = 0; e < StrLength(search); e++)
{
if (subject[i + e] == search[e])
{
calc
= StrAppendChar(calc, search[e]);
}
}
if (StrEquals(search, calc) == 0)
{
newstring
= StrAppend(newstring, replace);
i
= i + StrLength(search) - 1;
}
else
{
newstring
= StrAppendChar(newstring, subject[i]);
}
}
else
{
newstring
= StrAppendChar(newstring, subject[i]);
}
}
return newstring;
}
/* string replace maximal */
char* StrReplaceMax(char*
search, char* replace, char* subject, int count)
{
char* newstring = "";
int i = 0;
for (i = 0; i < StrLength(subject); i++)
{
if (subject[i] == search[0])
{
int e = 0;
char* calc = "";
for (e = 0; e < StrLength(search); e++)
{
if (subject[i + e] == search[e])
{
calc
= StrAppendChar(calc, search[e]);
}
}
if (StrEquals(search, calc) == 0)
{
if (count > 0)
{
newstring
= StrAppend(newstring, replace);
i = i + StrLength(search) - 1;
count = count - 1;
}
else
{
newstring
= StrAppendChar(newstring, subject[i]);
}
}
else
{
newstring
= StrAppendChar(newstring, subject[i]);
}
}
else
{
newstring
= StrAppendChar(newstring, subject[i]);
}
}
return newstring;
}
int main()
{
char* str = "this is a string
example";
int len_str = StrLength(str);
char* appned_str = StrAppend(str,
"append this
str");
char* append_char_str =
StrAppendChar(str, `#`);
char* replace_str = StrReplace("is", "was", str);
int equal_str = StrEquals(str,
replace_str);
printf("*** String Manipulation Using C ***\n\n");
printf("\nExample String : %s", str);
printf("\nString Length : %d", len_str);
printf("\nAppended String : %s", appned_str);
printf("\nAppended character : %s", append_char_str);
printf("\nReplaced String : %s", replace_str);
printf("\nis equal replaced
string : %d", equal_str);
getchar();
return 0;
}
Output: