Loading...
C String Functions

C String Functions

In this article, we will learn about library functions used for stringg manipulation in C programming such as gets(), puts(), strlen() etc with the help of examples.


String Library Functions

There are many string library functions which we can use according to our needs. These functions are used to make the program shorter and readable.

Note : string.h headers files used to handle all the string library functions.

Syntax

#include <stdio.h>

Commonly used string Library functions

Function Work of Function
strlen() Computes the length of the string
strcpy() copies a string to another string
strcat() concatenate or join two strings
strcmp() compare two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase

strlen()

The strlen() function is used to calculate the length of a given string.

Syntax

strlen(string);

The strlen() function takes a string as an argument and returns its length as a positive integer value.


Example : C strlen() function

#include <stdio.h>
#include <string.h>
int main() {
  char first[10]="Avengers";
  char second[10]={'A','v','e','n','g','e','r','s','\0'};
  printf("Length of string first is %d \n",strlen(first));
  printf("Length of string second is %d \n",strlen(second));
  return 0;
}

Output

Length of string first is 8 
Length of string second is 8

Note : The strlen() function doesn't count the null character \0 while calculating the length of the string.


strcpy()

The strcpy() function is used to copy a string to the pointed string and return the copied string.

Syntax

strcpy(destination_string, source_string);

The strcpy() function copy the data of source string in the destination string, and the previous data stored in the destination string is removed.


Example: C strcpy()

#include <stdio.h>
#include <string.h>
int main() {
  char favourite1[20] = "Tony Stark";
  char favourite2[20] = "Steve Jobs";
  // copying favourite1 to favourite2
  strcpy(favourite2, favourite1);
  puts(favourite2);   // C programming
  return 0;
}

Output

Tony Stark

Note: When we use strcpy() function then the size of the destination string should be large enough to store the copied string or it will gives us an warning "intialized array for array of chars is too long".


strcat()

The strcat() function is used to contcatenate or join two strings.

Syntax

strcat(destination_string, source_string);

The strcat() function concatenates the destination string and the source string, and the result is stored in the destination string.


Example: C strcat() function

#include <stdio.h>
#include <string.h>
int main() {
  char string1[50] = "This is right", string2[50] = " This is left";
  // concatenates or join string2 with string1
  strcat(string1, string2);
  puts(string1);
  puts(string2);
  return 0;
}

Output

This is right This is left
This is left

Note: When we use strcpy() function then the size of the destination string should be large enough to store the copied string or it will gives us an warning "intialized array for array of chars is too long".


strcmp()

The strcmp() function is used to compares two strings and returns 0. if both strings are same/identical.

The strcmp() function takes two strings and returns an integer.

The strcmp() compares two strings character by character.


strcmp() Return Values

Return Value Conditions
0 If both strings are identical/same
positive integer If the ASCII value of the first unmatched character is greater than the second.
negative integer If the ASCII value of the first unmatched character is lesser than the second.

Example: C strcmp() function

#include <stdio.h>
#include <string.h>
int main() {
  char string1[] = "abcd", string2[] = "ABCD", string3[] = "abcd";
  // comparing strings string1 and string2
  printf("strcmp(string1, string2) = %d\n", strcmp(string1, string2));
  // comparing strings string1 and string3
  printf("strcmp(string1, string3) = %d\n", strcmp(string1, string3));
  // comparing strings string2 and string3
  printf("strcmp(string2, string3) = %d\n", strcmp(string2, string3));
  return 0;
}

Output

strcmp(string1, string2) = 32
strcmp(string1, string3) = 0
strcmp(string2, string3) = -32

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of String Functions in C.

Keep Learning : )

In the next tutorial, you'll learn about C Files Input/Output.

- Related Topics