Skip to main content

Operations on the String in C Language

           C Language Beginner level

As we know , strings are the collection of characters , numbers , symbols and to store the strings using c program , we need to use Array ( collection of same data type values ) .

There may be various kind of operations possible over the strings -

1. Concatenation -
When we need to concatenate or simply add string with other , we use strcat() and strncat() function .

2. Length of String -
To calculate the length of any string s1 we use strlen() in beloe format -

strlen( s1 ) ;

Note -
>> This function returns the number of 
    characters available in the string .

>> To use this function , we need to include string.h header file in the program .

3. Copy Operation -

There are 3 type of library functions in c language to perform copy operation .
strcpy() , strncpy , strdup() .

To use these functions , we need to include string.h header file in the program .

Note :
To copy string s1 to string s2 using strcpy() , we write -

strcpy( s2 , s1 ) ;

strncpy() function can copy the desired number of characters .
If we want to copy only 5 characters of string s1 to string s2 , we write -

strncpy( s2 , s1 , 5 ) ;

4. Comparison of two Strings -

We use strcmp() library function to compare any two strings , character by character .
Syntex of this function is as below -

int strcmp( s1 , s2 ) ;

Here strcmp() compares two strings s1 and s2 .

If character by character comparison of strings is same then strcmp() returns binary number 1( true ) , otherwise 0 ( false ) .

5. String subset search -

If any string s1 is a proper subset of string s2 then strchr() library function tells the first position , where s1 found .

6. String conversion -

This is used to convert strings either from uppercase to lowercase or vice versa .
We use strlwr( s1 ) to convert the string s1 to lower case and strupr( s1 ) to convert the entire string s1 into upper case letters .

Next>>    <<Back

Comments