A C-library for string manipulations
Did you ever feel the need to manipulate a string in C? Have you ever wanted to create a substring, replace parts or split a string? If you did, now can do all of these things by using propaganda.c, the library for manipulating strings.
- Setup
- Usage
- Conclusion
To include the library in your project, you will need to import the propaganda.h
header file.
#include "propaganda.h"
// ...
In order to determine the length of a given string, you can call the strlen((char*) string)
function. It will return the length of your string, up to the '\0'
termination character.
// ...
char* str = "Hello World!";
int lenStr = strlen(str);
printf("Length of my string: %d\n", lenStr);
// ...
If you want to copy your string by value rather than copying it by reference, you can use the strcpy((char*) string)
function, which will return the reference to the copy.
// ...
char* str = "Lorem ipsum";
char* copy = strcpy(str);
copy[0] = 'A';
printf("My string copy: %s\n", copy);
printf("My original string: %s\n", str);
// ...
To make sure your string works with all of the library functions, and as a good convention, you should create all of your strings by using the newstr((char*) string)
function.
(Of course you could also use the strcpy((char*) string)
function, since it does the same thing.)
// ...
char* str = newstr("Hello World!");
printf("My string: %s\n", str);
// ...
In order to attach one string to another one, you should use the strcat((char*) str1, (char*) str2)
function. It returns a reference to the concatenated string.
// ...
char* str1 = newstr("Hello ");
char* str2 = newstr("World!");
printf("Concatenated: %s\n", strcat(str1, str2));
// ...
If you want to compare the value of two strings, use the strequals((char*) str1, (char*) str2)
function. It will return a value of either 0
or 1
.
// ...
char* str1 = newstr("Hello!");
char* str2 = newstr("Hello!");
printf("Are equal: %d\n", strequals(str1, str2));
str2 = newstr("Hello");
printf("Are still equal: %d\n", strequals(str1, str2));
// ...
You can create substrings in two different ways:
-
If you want to create a substring starting somewhere and lasting to the end, you can use the
substr((char*) string, (int) start)
function.
// ...
char* str = newstr("asdf Hello World!");
str = substr(str, strlen("asdf "));
printf("New string: %s\n", str);
// ...
-
You can however, also create a substring starting at some index, and ending at another index by using the
substring((char*) string, int start, int end)
function.It is to note, that the range of the substring will be [start;end[.
// ...
char* str = newstr("asdf Hello World! asdf");
str = substring(str, strlen("asdf "), strlen("asdf Hello World!"));
printf("New string: %s\n", str);
// ...
You might want to get the index of a specific character in a string, in order to do this, you can use the following functions:
-
In order to get the index of the first occurrence of the character, use the
indexOf((char*) str, (char) c)
function.
// ...
char* str = newstr("Hello World!");
int index = indexOf(str, 'o');
printf("First index: %d\n", index);
// ...
-
Sometimes, you might want to get the first occurrence of a character from a certain index. You can use the
indexOfFrom((char*) str, (char) c)
function to achieve this.
// ...
char* str = newstr("0123456789 0123456789");
int fIndex = indexOfFrom(str, '0', 10);
printf("First index from: %d\n", fIndex);
// ...
-
If you need to get the last index of the character, use the
lastIndexOf((char*) str, (char) c)
, which searches the string from the end.
// ...
char* str = newstr("This sentence has many 'a's ... ");
int lIndex = lastIndexOf(str, 'a');
printf("Last index: %d\n", lIndex);
// ...
Note: All indexOf...()
functions return a value of -1
if no match was found.
All functions that are available for characters are also available for substrings.
-
In order to get the index of the first occurrence of the string, use the
indexOfString((char*) str, (char*) sub)
function.
// ...
char* str = newstr("Hello World!");
int index = indexOfString(str, "World!");
printf("First index: %d\n", index);
// ...
-
Sometimes, you might want to get the first occurrence of a string from a certain index. You can use the
indexOfStringFrom((char*) str, (char*) sub)
function to achieve this.
// ...
char* str = newstr("0123456789 0123456789");
int fIndex = indexOfStringFrom(str, "89", 10);
printf("First index from: %d\n", fIndex);
// ...
-
If you need to get the last index of the substring, use the
lastIndexOfString((char*) str, (char*) sub)
, which searches the string from the end.
// ...
char* str = newstr("asdf hello hello asdf");
int lIndex = lastIndexOfString(str, "hello");
printf("Last index: %d\n", lIndex);
// ...
Note: All indexOfString...()
functions return a value of -1
if no match was found.
To get the amount of times a certain character appears in your string, use the charOccurrence((char*) str, (char) c)
function.
// ...
char* mystr = newstr("asjdflasdjdfkljaskldasdfaaaasdfasdfa");
int amount = charOccurrence(mystr, 'a');
printf("Amount of 'a's: %d\n", amount);
// ...
To get the amount of times a specified substring appears in your string, use the stringOccurrence((char*) str, (char*) sub)
function.
// ...
char* mystr = newstr("asdf qwer asdf qwer asdf qwer asdf qwer");
int amount = stringOccurrence(mystr, "asdf");
printf("Amount of substrings: %d\n", amount);
// ...
To split a string at a specified character, you can use the split((char*) str, (char) c)
function.
This will return a reference to a char-pointer (the first part). You should combine this with the charOccurrence(...)
function, to figure out, how many parts there are.
// ...
char* mystr = newstr("Hello! I'm Matthias ... Wow, this sentence is seemingly endless ...");
int amount = charOccurrence(mystr, ' ')+1;
char** parts = split(mystr, ' ');
for (int i = 0; i < amount; i++) {
printf(" [%02d]: %s\n", i, parts[i]);
}
// ...
To split a string at a specified substring, you can use the splits((char*) str, (char*) c)
function.
You should handle it the same way, as the split(...)
function's return value.
// ...
char* mystr = newstr("Hello!\r\nasdf\r\nqwer\r\n...\r\nWow,\r\nthis\r\nsentence\r\nis\r\nseemingly\r\nendless\r\n...");
int amount = stringOccurrence(mystr, "\r\n")+1;
char** parts = splits(mystr, "\r\n");
for (int i = 0; i < amount; i++) {
printf(" [%02d]: %s\n", i, parts[i]);
}
// ...
There are two ways to convert a string to lowercase:
The first, possible way would be to use the `lowerCase((char*) str)` function, which returns a lowercase-copy of the specified string. This is useful, if you don't want to modify the original "string".
// ...
char* mystr = newstr("Hello World!");
char* strLow = lowerCase(mystr);
printf("Lowercase: %s\n", strLow);
printf("Original: %s\n", mystr);
// ...
The second option would be, to convert the value of your "string" to lowercase using the `toLowerCase((char*) str)` function.
// ...
char* str = newstr("Hello everyone!");
toLowerCase(str);
printf("Original-lowercase: %s\n", str);
// ...
**Note:** When using the `toLowerCase(...)` function, it's necessary, that your string was created dynamically.
There are also two ways to convert a string to uppercase:
Similiar to the `lowerCase(...)` function, you can use the `upperCase((char*) str)`, which also returns a copy, but in this case the copy is in upper-case.
// ...
char* str = newstr("Hello World!");
char* upp = upperCase(str);
printf("Uppercase: %s\n", upp);
printf("Original: %s\n", str);
// ...
The second option, once again, converts the value of the "string" to uppercase. Use the `toUpperCase((char*) str)` function for this.
// ...
char* mystr = newstr("Lorem Ipsum");
toUpperCase(mystr);
printf("Original-uppercase: %s\n", mystr);
// ...
**Note:** When using the `toUpperCase(...)` function, it's necessary, that your string was created dynamically.
You can use the replace((char*) str, (char) c, (char) r)
function to replace all occurrences of the given character c
with the specified character r
.
// ...
char* mystr = newstr("Hello World!");
replace(mystr, 'o', 'a');
printf("String: %s\n", mystr);
// ...
You can use the replaceString((char*) str, (char*) s, (char*) r)
function to replace all occurrences of the given substring s
with the specified substring r
.
// ...
char* mystr = newstr("Hello World!");
mystr = replaceString(mystr, "World", "Humans");
printf("String: %s\n", mystr);
// ...
Use the trim((char*) str)
function to trim all whitespace from the beginning and end of a string.
// ...
char* msg = newstr(" \n\rHello World!\n ");
char* trimmed = trim(msg);
printf("Trimmed: %s\n", trimmed);
// ...
In order to convert an integer to its character representation, you can use the itoc((int) n)
.
// ...
int number = 8;
char n_chr = itoc(8);
printf("As character: %c\n", n_chr);
// ...
You can also do it the other way around by using the ctoi((char) c)
function.
// ...
char n_chr = '2';
int number = ctoi(n_chr);
printf("As integer: %d\n", number);
// ...
The following are all functions to convert several datatypes to a string.
-
To convert an integer to a string, use the
itos((int) n)
function.
int my_int = 802;
char* my_str = itos(my_int);
printf("String: %s\n", my_str);
-
To convert a boolean value to a string, you should use the
btos((int) b)
function.
int my_boo = 0;
char* my_str = btos(my_boo);
printf("String: %s\n", my_str);
-
To convert a long to a string, you can use the
ltos((long) n)
function.
// ...
long my_lon = 8000000l;
char* my_str = ltos(my_lon);
printf("String: %s\n", my_str);
// ...
-
To convert a float to a string, use the
ftos((float) n, (int) r)
function.Note: The
r
parameter specifies how many decimals to round to.
// ...
float my_flo = 3.141f;
char* my_str = ftos(my_flo, 3);
printf("String: %s\n", my_str);
// ...
-
To convert a double to a string, you should use the
dtos((double) n, (int) r)
function.Note: The
r
parameter specifies to how many decimals the resulting "string" should be rounded.
// ...
double my_dou = 3.14159265359;
char* my_str = dtos(my_dou, 10);
printf("String: %s\n", my_str);
// ...
The following are all functions to convert a string to several, different datatypes.
-
To convert a string to an integer, use the
stoi((char*) str)
function.
// ...
char* my_str = "1024";
int my_int = stoi(my_str);
printf("Integer: %d\n", my_int);
// ...
-
To convert a string to a boolean value, you should use the
stob((char*) str)
function.
// ...
char* my_str = "true";
int my_boo = stob(my_str);
printf("Boolean: %d\n", my_boo);
// ...
-
To convert a string to a long, you can use the
stol((char*) str)
function.
// ...
char* my_str = "8000000";
long my_lon = stol(my_str);
printf("Long: %ld\n", my_lon);
// ...
-
To convert a string to a float, use the
stof((char*) str)
function.
// ...
char* my_str = "3.141";
float my_flo = stof(my_str);
printf("Float: %f\n", my_flo);
// ...
-
To convert a string to a double, you can use the
stod((char*) str)
function.
// ...
char* my_str = "3.14159265359";
double my_dou = stod(my_str);
printf("Double: %.15lf\n", my_dou);
// ...
For any questions/suggestions, you can message me on my twitter.
... Matthias M.