Library functions for string

Library function is reserved functions inside compiler. There are lot of library function to manipulate string.
The most used library functions to manipulate string are discussed below:

gets() : It can be read as get string. We can input string by this function. Example:

char name[40];
gets(name);

Here input string will be assign to name.We can take input by scanf(); function.So a question may arise that- what is the difference between scanf() and gets() ?scanf() function will read string until a space encounter. When a space encounter rest of characters will be truncated

#include<stdio.h>
int main()
{
     char s[40];
     printf("Enter a sentence: ");
     scanf("%s",s);
     printf("Entered  sentence : %s",s);
     return 0;
}

OUTPUT

Enter a sentence: Hello world.
Entered sentence : Hello

Analysis:
Here s[40] can contain 39 character but only Hello have been assigned to ‘s’ because there is a space between Hello and world

We can input a sentence including space by gets().

#include<stdio.h>
#include<string.h>
int main()
{
     char s[40];
     printf("Enter a sentence: ");
     gets(s);
     printf("Entered  sentence : %s",s);
     return 0;
}

OUTPUT

Enter a sentence: Hello world.
Entered sentence : Hello world.

strlen():
It can be read as string length. It will return the number of character of a string.

#include<stdio.h>
#include<string.h>
int main()
{
    int length;
    char s[40];
    printf("Enter a word: ");
    scanf("%s",s);
    length=strlen(s);
    printf("Number of character in your input word: %d",length);
    return 0;
}

OUTPUT

Enter a word: Robin
Number of character in your input word: 5

strcat():
It can be read as string concatenate. This function have two arguments of string type. Second string will be appended at the end of the first string.
Explanation:

char string1[12]="Hello ";
char string2[12]="Rifat";

For the above code string1 and string2 can be imagined as follows:

string1

H
e
l
l
o
?
?
?
?
?
?

NOTE:

There is a space after Hello in string1. Space is also a character.

string2

R
i
f
a
t
/0
?
?
?
?
?
?

Now if you write

strcat(string1,string2);

Then string1 will be changed as follows while string2 will remain same.

string1

H
e
l
l
o
R
i
f
a
t
/o

string2

R
i
f
a
t
/0
?
?
?
?
?
?

Code example:

#include<stdio.h>
#include<string.h>
  int main()
 {
    char string1[20]="Hellow ";
    char string2[10]="Rifat";
    printf("Before executing strcat() : \n");
    printf("\nstring1 = %s",string1);
    printf("\nstring2 = %s",string2);

    strcat(string1,string2);

    printf("\n\nAfter executing strcat() : \n");
    printf("\nstring1 = %s",string1);
    printf("\nstring2 = %s",string2);
    return 0;
 }

OUTPUT

Before executing strcat() :

string1 = Hello
string2 = Rifat

After executing strcat() :

string1 = Hello Rifat
string2 = Rifat

strcpy():
It has two arguments of string type. Second string will be copied to the first string. First string will be replaced by second string while second string remain unchanged.Code example:

#include<stdio.h>
#include<string.h>
int main()
{
     char string1[30]="Hello";
     char string2[30]="World";
     printf("Before strcpy() function execution: \n");
     printf("string1 = %s\n",string1);
     printf("string2 = %s\n",string2);
     
     strcpy(string1,string2);
     printf("\n After strcpy() function execution: \n");
     printf("string1 = %s\n",string1);
     printf("string2 = %s",string2);
     return 0;
}

OUTPUT

Before strcpy() function execution:
string1 = Hello
string2 = World

After strcpy() function execution:
string1 = World
string2 = World

strcmp():
It can be read as string comparison.It has two arguments of string type. First argument will be compared with the second argument. It returns 0 or 1 or -1 based on 3 cases:
Case1:
If the numerical difference between first non matching characters of two string is greater than ZERO then 1 will be returned.

char string1="Santo";
char string2="Sajib";

Here first non-matching characters between Santo and Sajib are: n and j. Each character has a numeric value. Value of n is: 110 and value of j is : 106. So numeric deference between n and j is: 110-106 = 4 which is greater than ZERO. So 1 will be returned.

#include<stdio.h>
#include<string.h>
int main()
{
     char string1="Santo";
     char string2="Sajib";
     int r;
      r=strcmp(string1,string2);
      printf("%d",r);
      return 0;
}

OUTPUT

1

Case2:
If the numerical difference between first non matching characters of two string is less than ZERO then -1 will be returned.

char string2="Sajib";
char string1="Santo";

Here first non-matching characters between Santo and Sajib are: j and n.Value of j is: 106 and value of n is : 110. So numeric deference between jand n is: 106-110 = -4 which is less than ZERO. So -1 will be returned.

#include<stdio.h>
#include<string.h>
int main()
{
      char string2="Sajib";
      char string1="Santo";
      int r;
      r=strcmp(string1,string2);
      printf("%d",r);
      return 0;
}

OUTPUT

-1

Case3:
If two string are equal then ZERO(0) will be returned.

#include<stdio.h>
#include<string.h>
int main()
{
      char string2="Robin";
      char string1="Robin";
      int r;
      r=strcmp(string1,string2);
      printf("%d",r);
      return 0;
}

OUTPUT

0

List of numeric value for all capital character
Character Numerical value
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
List of numeric value for all small characters
Character Numerical value
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122

NOTE: string.h header file is needed for gets(), strlen(),strcat(), strcmp().


Next Previous