String Examples




Substring Extraction

Trim Trailing Whitespace



This function removes trailing whitespace (spaces and tabs) from a string. It works by iterating from the end of the string and replacing whitespace characters with null terminators until a non-whitespace character is encountered.




String Formatting

Convert To Uppercase



This code converts all lowercase letters in a string to uppercase. It uses the toupper function from <ctype.h> and iterates through each character of the string using a pointer.

String Padding



This code pads a string with leading spaces to reach a desired length.

String To Lowercase



This code converts all uppercase letters in a string to lowercase using ASCII value manipulation.




String Manipulation

Reverse String



This function reverses a string in-place. It uses two pointers, one starting from the beginning and one from the end, swapping characters as it moves towards the center of the string.

Copy String



This snippet copies a string from src to dest, including the null terminator. It's similar to the standard strcpy function, using pointer arithmetic to efficiently copy characters one by one.

String Length



This code calculates the length of a string manually by counting characters until the null terminator.

String Comparison



This code compares two strings manually, character by character, returning the difference between the first non-matching characters.




String Searching

Find First Occurrence



This code finds the first occurrence of a specific character in a string. It returns a pointer to the character if found, or NULL if not found. The function iterates through the string, comparing each character to the target.

Count Substring Occurrences



This function counts the number of times a substring appears within a larger string using the strstr function.

String Palindrome Check



This code checks if a string is a palindrome by comparing characters from both ends towards the middle, ignoring case.




String Concatenation

String Concatenation



This function concatenates two strings by appending the source string to the destination string.




String Tokenization

String Tokenization



This code demonstrates string tokenization using strtok(). It splits a string into tokens based on a delimiter.




String Compression/decompression

String Compression



This code performs basic string compression by replacing repeated characters with the character followed by its count.

Related Examples