🔸Strings Manipulation
Summary
String representation
char word[7] = {"hellow"}; → always use double quotes and char type
or
char word[7] = "hellow"; → can remove brakets
or
char word[] = "hellow"; → this one is better
String functions
https://en.cppreference.com/w/c/string/byte
the functions with an extra "n" in their names are safer to use because they check for size and can prevent errors or buffer overflow.
strlen() → get the length of a string: strlen(mystring)
strcpy(s1,s2) / strncpy(s1,s2,n) → copy one character string to another (replace)
n is usually the length of first variable replace first variable content with second one
strcat(s1,s2) / strncat(s1,s2,n) → combining two character strings together and puts the result in the first one: n is usually the length of first variable
strcat(first,second); → a copy of second string is appended to the first string and the result is saved to the first string, second one is not altered
strcmp(s1,s2) / strncomp(s1,s2,n) → determing if two strings are equal n is usually the length of first variable this function does for string what relational operators do for numbers: if it returns 0, the strings are same and none-zero if it returns < 0 (negative value) then string1 is less than string2 if it returns > 0 (posetive value) then string2 is less than string1
the "less than" here means in alphabetic order for example:
memchr() → searches an array for the first occurrence of a character
example:
Output:
search character found: DEFG
memcmp() → compare the first n bytes of two strings if equal, returns 0 if var1 > var2 returns positive if var2 > var1 returns negative n is the number of first bytes to compare.e.g: size_t 10
size_t strlen(s) → returns the number of characters in s, excluding NULL character
strchr() → searchs a given string for a specified character first arg is the string to be searched (the address of a char array) second arg is the character to search for the function will search the string starting at the beginning and return a pointer to the first position in the string where character is found. the address of this position in memory is of type char* described as the pointer to char.
to store the value that's returned you must create a variable that can store the address of a character.
if the character is not found the function returns a special value NULL. NULL is the equivalent of 0 for a pointer and represents a pointer that does not point to anything.
example:
String after |.| is - |.tutorialspoint.com|
strrchr(string,character) → searchs for the last occurence of the character c in string.
strstr() → the most usefull searching function, searchs one string for the first occurrence of a substring , returns a pointer to the position in the first string where the substring is found, if no match, returns NULL.
example:
strtok() → function is used for tokenizing strings a token is a sequence of characters within a string that is bound by a delimiter. a delimiter can be anything, but should be unique to the string. spaces, commas, and periods are good examples.
breaking a sentence into words is called tokenizing
example:
to extract all tokens:
c = getchar(); → gets a single character as input
putchar(c) → prints a single character as output
example:
example:
Reversing a string
Analyzing strings
example:
Finding mixed character strings (without string.h)
Last updated