0% found this document useful (0 votes)
39 views2 pages

C String Functions Explained

Uploaded by

phamdomanhduc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

C String Functions Explained

Uploaded by

phamdomanhduc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

String Copying

• strcpy(dest, src):
Copies the entire string from src to dest, including the null terminator. It's
important to ensure dest has enough space to hold the entire copied string
and null terminator.

• strncpy(dest, src, num):


Copies at most num characters from src to dest, including the null
terminator if it fits within num characters. Caution: This function might
not always add the null terminator if num reaches the destination string
size. Ensure manual addition if needed.

2. String Concatenation

• strcat(dest, src):
Appends the string src to the end of the string dest. Note that dest must have
enough space to hold the combined string and null terminator.

• strncat(dest, src, num):


Appends at most num characters from src to the end of the string dest, but
unlike strcat, it stops appending even if src has less than num characters.
Caution: Similar to strncpy, strncat might not always add the null
terminator if num reaches the remaining space in dest. Ensure manual
addition for safety.

3. String Comparison

• strcmp(str1, str2):
Compares two strings (str1 and str2) and returns 0 if they are equal. It
returns a negative value if str1 is less than str2 alphabetically (based on the
first differing character) and a positive value if str1 is greater than str2.

• strncmp(str1, str2, num):


Compares at most num characters of str1 and str2. It works similarly to
strcmp but only considers the first num characters.
4. String Searching

• strchr(str, ch):
Locates the first occurrence of the character ch (an unsigned char) within
the string str and returns a pointer to that character. If ch is not found, it
returns a null pointer.

• strrchr(str, ch):
Similar to strchr, but it finds the last occurrence of the character ch within
the string str and returns a pointer to that character, or a null pointer if not
found.

• strstr(str1, str2):
Searches for the first occurrence of the substring str2 within the string str1
and returns a pointer to the beginning of that substring within str1. If str2 is
not found, it returns a null pointer.

5. Other String Manipulation

• strlen(str):
Returns the length of the string str as the number of characters excluding
the null terminator.

• memset(str, ch, size):


Fills a block of memory pointed to by str with a given character ch for size
number of bytes. This can be useful for initializing strings with a specific
character, like null characters (\0).

You might also like