C Programming

How to Perform String Manipulation in C Programming?

Manipulating strings is a common task in C programming, which refers to the process of changing or processing the string’s content, which is a sequence of characters terminated by a null character ‘\0’. This article is a detailed guide to manipulate strings in C programming, including declaring and initializing strings and performing basic operations such as copying, comparing, and concatenating strings in addition to assigning string values to variables.

String Manipulation in C Programming

In the C programming language, the first step to manipulating strings is assigning a string value to a variable. An array of characters with a null character (represented by 0) at the end is how a string is expressed in C.

The following example shows an example of assigning a string in C and printing it at the console.

#include <stdio.h>

int main () {

  char msg[10] = {'L', 'i', 'n', 'u', 'x', 'h', 'i', 'n', 't', '\0'};

printf("Printed message: %s\n", msg );

  return 0;

}

The provided code declares and initializes a character array named “msg” with a length of 10 characters. The contents of the array are the letters “L,” “i”, “n”, “u”, “x”, “h”, “i”, “n”, “t”, followed by a null terminator represented by the value 0. Next, the printf() function is used to display the message “Printed message:” to the console, followed by the contents of the “msg” array using the “%s” format specifier, which is used to print strings.

Output

Functions for String Manipulation

C programming provides a variety of built-in functions and libraries for performing string manipulation. These include strlen(), strcpy(), strcat(), and strcmp(), among others. Each of these functions serves a distinct purpose, but they all work towards the same goal of manipulating strings.

By utilizing these functions in combination, programmers can efficiently manipulate strings in their C programs. However, before utilizing these functions, it is important to have a solid understanding of their syntax and usage to ensure that they are used correctly and effectively.

1: strlen()

To calculate the length of a given string in C programming, we use the strlen() function. It takes a string as an argument and returns the length of the string as a size_t value, which is an unsigned integer type.

The strlen() function works under the header file “<string.h>”. The syntax for using strlen() is as follows:

size_t strlen(const char *str);

Here, “size_t” is the return type of the function, “strlen” is the name of the function, and “const char *str” is the argument passed to the function, which is a pointer to the string whose length is to be determined.

2: strcpy()

One common task in string manipulation is to copy the contents of one string variable to another. In C programming, this can be accomplished using the strcpy() function.

The syntax for using strcpy() is as follows:

char *strcpy(char *dest, const char *src);

Here, “char *” is the return type of the function, “strcpy” is the name of the function, “dest” is the destination string variable to which the source string will be copied, and “src” is the source string variable.

3: strcmp()

Comparing two strings lexicographically is a frequent string manipulation task in C programming. A built-in function for comparing two strings is called strcmp().

The syntax for using strcmp() is as follows:

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

Here, “int” is the return type of the function, “strcmp” is the name of the function, “str1” and “str2” are the two string variables to be compared.

4: strcat()

In C programming, concatenating strings is another common task in string manipulation. String concatenation involves combining two or more strings into a single string. An inbuilt function called strcat() is used to combine two strings.

The syntax for using strcat() is as follows:

char *strcat(char *dest, const char *src);

Here, “char *” is the return type of the function, “strcat” is the name of the function, “dest” is the destination string variable to which the source string will be concatenated, and “src” is the source string variable.

Note: To use all these functions in C Programming, you must add <string.h> header file at the start of your code.

Here is a complete program that demonstrates some common string manipulation tasks in C Programming.

#include <stdio.h>

#include <string.h>

int main() {
    char str1[100], str2[100], concat[200];

printf("Enter the first string: ");
scanf("%s", str1);

printf("Enter the second string: ");
scanf("%s", str2);

sprintf(concat, "%s%s", str1, str2);
printf("The concatenated string is: %s\n", concat);

printf("The length of the concatenated string is: %d\n", strlen(concat));

    char copy[200];
strcpy(copy, concat);
printf("The copied string is: %s\n", copy);

    int result = strcmp(str1, str2);

    if (result == 0) {
printf("The two strings are equal.\n");
    }
    else if (result < 0) {
printf("The first string is less than the second string.\n");
    }
    else {
printf("The first string is greater than the second string.\n");
    }

    return 0;
}

This above program asks the user to enter two strings, join them together into one string, then carry out numerous string operations. The program uses the sprintf() function to concatenate the two strings with the help of the concat() function. It then uses the strlen() function to determine the length of the resulting string, strcpy() to make a copy of the concatenated string, and strcmp() to compare the two input strings using the if-else statement.

Output

Conclusion

Performing string manipulation in C programming involves assigning a string value to a variable, and copying, comparing, and concatenating strings. Each of these string manipulation tasks is covered by several functions in the C computer language. By mastering these functionalities, programmers can efficiently manipulate strings in their C programming projects, making their code more efficient and effective.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.