
String is a sequence of characters that ends with the null character, which is a special character (“\0”). The term “string” refers to a collection of characters. In the C programming language, the string array is a popular two-dimensional array. When we talk about an array of characters, we’re talking about something similar to a string array. We use a null character at the end of each string.
Table of Contents
Syntax for a String array
char_datatype array_name[No.of strings][size of each string];
Initializing a String array
char name[3][10] = {"It","is","String"};
Functions of String in C
Functions | Description |
strcat() | It’s used to concatenate (join) two strings together. |
strlen() | It’s used to display how long a string is. |
strrev() | It’s used to reverse a string. |
strcpy() | One string is copied into another. |
strcmp() | It’s used to do a comparison between two strings. |
strupr() | The strupr() method converts a string to uppercase. |
strlwr() | The strlwr () method converts a string to lowercase. |
strcat()
Strings str1 and str2 are concatenated in this function, and string str2 is added to the end of string str1.
Syntax:
strcat(str1, str2);
Example:
strcat("Soft","ware");
Output: Software
strlen()
By using strlen() method we find the length of the string. Here length means the number of characters present in the string.
Syntax:
strlen(str);
Example:
strlen("GeekCer")
strrev()
Syntax:
strrev(str);
Example:
strrev("GeekCer")
strcpy()
The strcpy() method copies a string’s contents into another.
Syntax:
strcpy(str1,str2); /* Copies str2 into str1*/
Example:
strcpy(str,"GeekCer"); /* Copies "GeekCer" to str*/
strcmp()
The strcmp() is a function that compares two strings str1 and str2 to see if they are identical or dissimilar. Internally, two strings are compared character by character.
Strcmp() returns 0 if the two strings are equal. If they aren’t the same, the result returned is the numerical difference between the ASCII values of the first pair of characters.
Syntax:
strcmp(str1, str2);
Example:
strcmp("Soft","ware");
strupr()
The strupr() function converts the case of the specified string to upper case. For example, If you specify “geekcer” this method will change it to “GEEKCER.”
Syntax:
strupr(str);
Example:
strupr("GeekCer")
strlwr()
The strlwr() function converts the case of the string to lower case. For example, If you specify “GEEKCER” this method will change it to “geekcer”.
Syntax:
strlwr(str);
Example:
strlwr("GeekCer")
Click here to learn basics C programming
Pass string as parameters in C
The countStr() is a function in this program that counts the number of characters in a string. We’re sending char array as a method argument since string is just a character array.
#include<stdio.h>
int countStr(char str[]) {
int i = 0;
while (str[i] != '\0') {
i++;
}
return i;
}
int main() {
char str[18] = "GeekCer Education";
int count = 0;
count = countStr(str);
printf("Characters are : %d", count);
return 0;
}