Week 9: Characters and Strings (2)
String-Manipulation Functions of the String-Handling Library
The String-Manipulation Functions in the String-Handling Library (<string.h>) provide a set of tools for manipulating strings efficiently. For example,
The string-handling library in C facilitates manipulation of string data through functions like copying strings (
strcpy) and concatenating strings (strcat).String comparison operations are supported via functions such as
strcmp, allowing for easy comparison of two strings.Searching functionality is provided through functions like
strstr, enabling users to search for characters or other strings within a given string.Tokenization of strings, separating them into logical pieces, is made possible by functions like
strtok, allowing for parsing of input data.Determining the length of strings is supported by the
strlenfunction, which returns the number of characters in a string excluding the null terminator.
Example: basic string operation
Try to run the following example, and see if you understand the output:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello";
char destination[20] = "";
char string1[] = "Hello";
char string2[] = "World";
char search[] = "lo";
// Example of strcpy
strcpy(destination, source);
printf("Copied string: %s\n", destination);
// Example of strcat
strcat(destination, " World!");
printf("Concatenated string: %s\n", destination);
// Example of strcmp
int compare = strcmp(string1, string2);
if(compare == 0)
printf("Strings are equal.\n");
else if(compare < 0)
printf("String1 is less than String2.\n");
else
printf("String1 is greater than String2.\n");
// Example of strstr
char *substring = strstr(source, search);
if(substring != NULL)
printf("'%s' found in '%s' at position: %ld\n", search, source, substring - source);
else
printf("'%s' not found in '%s'\n", search, source);
return 0;
}Do you understand when the output says "String1 is less than String2"?
In C, when we say that "String1 is less than String2," we're referring to the result of comparing the strings using functions like strcmp.
The comparison is based on lexicographical ordering (aka dictionary order). In simple terms, it means comparing strings character by character from left to right until a difference is found or one string ends.
If the first differing character in string-1 has a lower ASCII value than the corresponding character in string-2, then string-1 is considered "less" than string-2. Conversely, if the ASCII value of the differing character in string-1 is higher, then string-1 is considered "greater" than string-2.
For example:
"apple" is less than "banana" because 'a' comes before 'b' in the ASCII table.
"bat" is less than "bats" because even though the first three letters are the same, the shorter string is considered "less" than the longer one.
Keep in mind that this comparison is case-sensitive, so uppercase letters have different ASCII values than their lowercase counterparts.
If you would like to do a compare up to n characters:
Example on tokenize the string:
Note: strtok modifies the original string by inserting '\0' characters to separate tokens. Also, calling strtok with a NULL pointer as the first argument tells it to continue tokenizing the same string from where it left off.
Search Functions of the String-Handling Library
Function prototypes and descriptions
char *strchr(const char *s, int c);
Locates the first occurrence of character c in string s. If c is found, strchr returns a pointer to c in s. Otherwise, a NULL pointer is returned.
size_t strcspn(const char *s1, const char *s2);
Determines and returns the length of the initial segment of string s1 consisting of characters not contained in string s2.
size_t strspn(const char *s1, const char *s2);
Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.
char *strpbrk(const char *s1, const char *s2);
Locates the first occurrence in string s1 of any character in string s2. If a character from s2 is found, strpbrk returns a pointer to that character in s1. Otherwise, it returns NULL.
char *strrchr(const char *s, int c);
Locates the last occurrence of c in string s. If c is found, strrchr returns a pointer to c in string s. Otherwise, it returns NULL.
char *strstr(const char *s1, const char *s2);
Locates the first occurrence in string s1 of string s2. If the string is found, strstr returns a pointer to the string in s1. Otherwise, it returns NULL.
Example
Memory Functions of the String-Handling Library
The memory functions in the String-Handling Library provide a set of tools for manipulating memory efficiently. These functions include operations such as copying memory, filling memory with a constant byte, comparing memory blocks, or searching for a byte in a memory block. They are essential for tasks involving low-level memory operations, data copying, and memory manipulation in C programming.
Function prototypes and brief descriptions for memory functions:
memcpy:
Description: Copies
nbytes from the memory area pointed to bysrcto the memory area pointed to bydest. The memory areas must not overlap.
memmove:
Description: Copies
nbytes from the memory area pointed to bysrcto the memory area pointed to bydest. The memory areas may overlap, and the copy is done in a non-destructive manner.
memcmp:
Description: Compares the first
nbytes of the memory areas pointed to bys1ands2. Returns an integer less than, equal to, or greater than zero ifs1is found to be less than, equal to, or greater thans2, respectively.
memchr:
Description: Searches for the first occurrence of the character
c(interpreted as an unsigned char) in the firstnbytes of the memory area pointed to bys. Returns a pointer to the matched byte orNULLif the character is not found.
memset:
Description: Sets the first
nbytes of the memory area pointed to bysto the value ofc(interpreted as an unsigned char).
Example:
When will you use memset?
memset?memset is used to initialized dynamically allocated memory or arrays to a specific value. Suppose you're developing a program that involves processing data in large arrays. Before you start using these arrays, you need to ensure that they are properly initialized to avoid any unexpected behavior. For instance, you might need to initialize an array of integers to all zeros or set all characters in a buffer to a specific value before using it to store data.
In this example, memset is used to initialize the dynamically allocated array array to all zeros. This ensures that all elements of the array are set to a known initial state before further processing. This practice helps prevent uninitialized memory bugs and ensures predictable behavior of your program.
Classwork - Week 9
Part 1:
Dates are commonly printed in several different formats in business correspondence. Two of the more common formats are 03/18/2024 and 18 March 2004. Write a program that reads a date in the first format and prints it in the second format.
Part 2:
Read and summarize about Cryptography. This will be one of problem for group programming.
Last updated