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:

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

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().

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.

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:

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

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:

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:

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.

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.

OUTPUT

1

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

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.

OUTPUT

-1

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

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