String handling functions

C supports a wide range of string manipulation functions. This sections will discuss some of the most commonly used string functions.

a. Finding the length of the given string

we know that every string must have ‘\0’ (null character) as the last character. the length of string can be calculated using strlen () library function. Without using strlen () function we can calculate the length of the string by counting the number of characters present in a string.


b. String concatenation

By concatenation we mean to add two (or more) strings place them in the first string. First, we must move our index to the last character of the first string. From that the first character of the second string is added to the first string. To achieve this, we must increment both the strings by one position. Finally string one will have concatenated string. We can also perform string concatenation with using strcat () library function.


c. String reverse

This function is also most commonly used. For example, this function is used to check whether the given is palindrome or not. In this function we calculate the length of the first character of the second string. Here we are using one decrement operator to decrement the position of the first string, and one increment operator to increment the position.


d. String copy

The program to the copy a string to another string shown below. After getting the string form the user, the input string is copied character by character to the destination string till the first encounters the null character.


e. Combining string together

Just as in any arithmetic addition, we cannot join (or) combine two strings.

string3=string1+string2

is invalid

In the string joining, the character from string1 and string2 must be copied into string3 one after another and they array, size of string3 must be enough to hold the total characters.


f. String comparison

Here we are going to check whether the given two strings are equal or not. First we have to give both the strings, to be compared. The algorithm used for comparing the two strings is to compare them character by – character and also to check for the string which does not reach the null character. If both the strings are equal, the while loop completes its cycle and comes out. And at the stage both the strings are at the null character, resulting to display both the string are equal. If either of the string does not match with the other string loop will break. And at this stage naturally one string cannot coincide with the other resulting to display the strings are unequal.


g. Extraction of one string from another string

The counterpart for the above function in BASIC is MIDS and in PASCAL it is COPY. First we have to enter the source string, and then starting position, and number of characters to extract from the string are given through the variable i and j. Since the array starts from 0, it is necessary to decrement the value of starting position by one. Now the value of starting position and number of characters to be extract is added and it is stored in variable m. the initial value of for loop is set to i, and final value is set to m. The putchar () is used to print character by character till index is less than m. Thus we can extract a string from another string.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext