Strings and String functions in c

String is an array of characters. In this post, we learn how to declare strings, how to work with strings in C programming and how to use the pre-defined string handling functions.

We will see how to compare two strings, concatenate strings, copy one string to another & perform various string manipulation operations. We can perform such operations using the pre-defined functions of “string.h” header file. In order to use these string functions you must include string.h file in your C program.

String Declaration

CString

Method 1:

char address[]={'T', 'E', 'X', 'A', 'S', '\0'};

Method 2: The above string can also be defined as

char address[]="TEXAS";

In the above declaration NULL character (\0) will automatically be inserted at the end of the string.

What is NULL Char “\0”?
'\0' represents the end of the string. It is also referred as String terminator & Null Character.

String I/O in C programming

C String-IO

C – String functions

C string-functions

C String function – strlen

Syntax:

size_t strlen(const char *str)

size_t represents unsigned short
It returns the length of the string without including end character (terminating char ‘\0’).

strlen vs sizeof
strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array.

C String function – strnlen

Syntax:

size_t strnlen(const char *str, size_t maxlen)

size_t represents unsigned short
It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

C String function – strcmp

int strcmp(const char *str1, const char *str2)

It compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison.

If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value.
If string1 == string2 then you would get 0(zero) when you use this function for compare strings.

C String function – strncmp

int strncmp(const char *str1, const char *str2, size_t n)

size_t is for unassigned short
It compares both the string till n characters or in other words it compares first n characters of both the strings.

C String function – strcat

char *strcat(char *str1, char *str2)

It concatenates two strings and returns the concatenated string.

C String function – strncat

char *strncat(char *str1, char *str2, int n)

It concatenates n characters of str2 to string str1. A terminator char (‘\0’) will always be appended at the end of the concatenated string.

C String function – strncpy

char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned short and n is a number.
Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends several terminator chars(‘\0’) to accumulate the length of str1 to make it n.

C String function – strchr

char *strchr(char *str, int ch)

It searches string str for character ch (you may be wondering that in above definition I have given data type of ch as int, don’t worry I didn’t make any mistake it should be int only. The thing is when we give any character while using strchr then it internally gets converted into integer for better searching.

C String function – Strrchr

char *strrchr(char *str, int ch)

It is similar to the function strchr, the only difference is that it searches the string in reverse order, now you would have understood why we have extra r in strrchr, yes you guessed it correct, it is for reverse only.

C String function – strstr

char *strstr(char *str, char *srch_term)

It is similar to strchr, except that it searches for string srch_term instead of a single char.