In C programming, a string is a sequence of characters terminated with a null character ‘\0’.
Ex: char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double quotation marks,
it appends a null character \0 at the end by default.
Memory diagram of strings in C programming.
How to declare a string?
Declaration of strings: Declaring a string is as simple as declaring a one-dimensional array.
Below is the basic syntax for declaring a string.
char str_name[size];
Here's how you can declare strings:
char s[5];
we have declared a string of 5 characters.
How to initialize strings?
You can initialize strings in a number of ways.
char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};
String I/O function:
Read & write Strings in C using Printf() and Scanf() functions
#include <stdio.h>
#include <string.h>
void main()
{
/* String Declaration*/
char nickname[20];
printf("Enter your Nick name:");
scanf("%s", nickname);
/*Displaying String*/
printf("%s",nickname);
}
Output:
Enter your Nick name: Negan
Negan
Note: %s format specifier is used for strings input/output
Read & Write Strings in C using gets() and puts() functions
#include <stdio.h>
#include <string.h>
void main()
{
/* String Declaration*/
char nickname[20];
/* Console display using puts */
puts("Enter your Nick name:");
/*Input using gets*/
gets(nickname);
puts(nickname);
}
Output:
Enter your Nick name: Negan
Negan
We need to often manipulate strings according to the need of a problem. Most, if not all, of the
time string manipulation can be done manually but, this makes programming complex and large.
To solve this, C supports a large number of string handling functions in the standard library
"string.h".
Function Work of Function
strlen() computes string's length
strcpy() copies a string to another string
strncpy() copies n characters from a string to another string
strcat() Concatenates a string at the end of another string
strncat() Concatenates only n characters of a string to another string
strcmp() Compares two strings
strncmp() Compares n characters of a string with another string
strlwr() converts string to lowercase
strupr() converts string to uppercase
strrev() Reveres the given string
C String function – strlen
It returns the length of the string without including end character (terminating char ‘\0’).
Syntax:
size_t strlen(const char *str)
size_t represent sunsigned short
Example of strlen:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1: %d", strlen(str1));
return 0;
}
Output:
Length of string str1: 13
C String function – strcmp
It compares the two strings and returns an integer value. If both the strings are same (equal) then
this function would return 0 otherwise it may return a negative or positive value based on the
comparison. If string1 < string2 OR string1 is a substring of string2 then it would result in a
negative value. If string1 > string2 then it would return positive value.
If string1 == string2 then you would get 0(zero) when you use this function for compare strings.
Syntax:
int strcmp(const char *str1, const char *str2)
Example of strcmp:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:
string 1 and 2 are different
C String function – strncmp
It both the string till n characters or in other words it compares first n characters of both the
strings.
Syntax:
int strncmp(const char *str1, const char *str2, size_t n)
size_t is for unassigned short
Example of strncmp:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
/* below it is comparing first 8 characters of s1 and s2*/
if (strncmp(s1, s2, 8) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:
string1 and string 2 are equal
C String function – strcat
It concatenates two strings and returns the concatenated string.
Syntax:
char *strcat(char *str1, char *str2)
Example of strcat:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
Output:
Output string after concatenation: HelloWorld
C String function – strncat
It concatenates n characters of str2 to string str1. A terminator char (‘\0’) will always be
appended at the end of the concatenated string.
Syntax:
char *strncat(char *str1, char *str2, int n)
Example of strncat:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strncat(s1,s2, 3);
printf("Concatenation using strncat: %s", s1);
return 0;
}
Output:
Concatenation using strncat: HelloWor
C String function – strcpy
It copies the string str2 into string str1, including the end character (terminator char ‘\0’).
Syntax:
char *strcpy( char *str1, char *str2)
Example of strcpy:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}
Output:
String s1 is: string 2: I’m gonna copied into s1
C String function – strncpy
Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends several
terminator chars(‘\0’) to accumulate the length of str1 to make it n.
Syntax:
char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned short and n is a number.
Example of strncpy:
#include <stdio.h>
#include <string.h>
int main()
{
char first[30] = "string 1";
char second[30] = "string 2: I’m using strncpy now";
/* this function has copied first 10 chars of s2 into s1*/
strncpy(s1,s2, 12);
printf("String s1 is: %s", s1);
return 0;
}
Output:
String s1 is: string 2: I’m
C String function – strupr
The strupr( ) function is used to converts a given string to uppercase.
Syntax:
char *strupr(char *str);
Parameter:
str: This represents the given string which we want to convert into uppercase.
Returns: It returns the modified string obtained after converting the characters of the given
string str to uppercase.
Example 1:-
// c program to demonstrate example of strupr() function.
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "Hello World";
//converting the given string into uppercase.
printf("%s\n", strupr (str));
return 0;
}
Output:
HELLO WORLD
C String function – strlwr
The strlwr( ) function is a built-in function in C and is used to convert a given string into
lowercase.
Syntax:
char *strlwr(char *str);
Parameter:
str: This represents the given string which we want to convert into lowercase.
Returns: It returns the modified string obtained after converting the characters of the given
string str to lowercase.
/ C program to demonstrate
// example of strlwr() function
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "HELLO WORLD";
// converting the given string into lowercase.
printf("%s\n",strlwr (str));
return 0;
}
Output:
hello world
C String function – strrev
strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given
below.
Syntax:
char *strrev(char *string);
#include<stdio.h>
#include<string.h>
void main()
{
char name[30] = "Hello";
printf("String before strrev( ) : %s\n",name);
printf("String after strrev( ) : %s",strrev(name));
}
OUTPUT:
String before strrev( ) : Hello
String after strrev( ) : olleH
Write a C program to find length of a string without using string library function.
#include <stdio.h>
void main()
{
char str[50];
int i, length = 0;
printf("Input a string : ");
scanf("%s", str);
for (i = 0; str1[i] != '\0'; i++)
{
length ++;
}
printf("The string contains %d number of characters. \n", length);
printf("So, the length of the string %s is : %d\n\n", str, length);
}
Sample Output:
Input a string: welcome
The string contains 7 numbers of characters.
So, the length of the string welcome is: 7
Write a C program to copy content of one string to another without string library
functions.
#include <stdio.h>
void main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
gets(s1);
for (i = 0; s1[i] != '\0'; ++i)
s2[i] = s1[i];
s2[i] = '\0';
printf("String s2: %s", s2);
}
Enter string s1: Hey fellow programmer.
String s2: Hey fellow programmer.
Write a C program to concatenate string1 to string2 without using string library function.
#include<stdio.h>
#include<string.h>
void main()
{
char s1[50], s2[30];
int i,j;
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
while (s2[i]!=’\0’)
i++;
for (j = 0; s1[j] != '\0'; i++, j++) {
s2[i] = s1[j];
}
s2[i] = '\0';
printf("\nConcated string is :%s", s2);
}
Output:
Enter String 1 : Ankit
Enter String 2 : Singh
Concated string is : AnkitSingh
Write C program to compare two strings without using string library function.
#include<stdio.h>
int stringCompare(char[], char[]); // function prototype declaration
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char s1[100], s2[100];
int compare;
printf("\n\nEnter 1st string: ");
scanf("%s", s1);
printf("\n\nEnter 2nd string: ");
scanf("%s", s2);
compare = stringCompare(s1, s2); // function call
if(compare == 1)
printf("\n\nBoth the strings are exactly same.\n\n");
else
printf("\n\nBoth the strings are different.\n");
return 0;
}
int stringCompare(char str1[], char str2[]) // function definition
{
int i = 0, flag = 0;
while(str1[i] != '\0' && str2[i] != '\0') // until atleast one string ends
{
if(str1[i] != str2[i])
{
flag = 1;
break;
}
i++;
}
/*
If all the characters are sequentially same as well as
both strings have ended
*/
if(flag == 0 && str1[i] == '\0' && str2[i] == '\0')
return 1;
else
return 0;
}