Chapter 13 Strings
Chapter 13 Strings
Fundamentals and
Programming in C
2nd Edition
Reema Thareja
1
© Oxford University Press 2016. All rights reserved.
CHAPTER 13
STRINGS
str[0] 1000 H
str[1] 1001 E
str[2] 1002 L
str[3] 1003 L
str[4] 1004 O
str[5] 1005 \0
The string can also be written by calling the putchar() repeatedly to print a sequence of single characters
i=0;
while(str[i] != '\0*)
{ putchar(str[i]);
i++;
}
• In memory the ASCII code of a character is stored instead of its real value. The ASCII code for
A-Z varies from 65 to 91 and the ASCII code for a-z ranges from 97 to 123. So if we have to
convert a lower case character into upper case, then we just need to subtract 32 from the ASCII
value of the character.
• In order to extract a substring from the right side of the main string we need to first calculate the
position. For example, if S1 = “Hello World” and we have to copy 7 characters starting from the right,
then we have to actually start extracting characters from the 5th position. This is calculated by, total
number of characters – n + 1.
• For example, if S1 = “Hello World”, then Substr_Right(S1, 7) = o World
The insertion operation inserts a string S in the main text, T at the kth position. The general syntax of this
operation is: INSERT(text, position, string). For ex, INSERT(“XYZXYZ”, 3, “AAA”) = “XYZAAAXYZ”
• Index operation returns the position in the string where the string pattern first occurs. For example,
• INDEX(“Welcome to the world of programming”, “world”) = 15
• However, if the pattern does not exist in the string, the INDEX function returns 0.
R A M ‘\
Name[0]
0’
Name[1] M O H A N ‘\
0’
Name[2] S H Y A M ‘\
0’
Name[3] H A R I ‘\
0’
Name[4] G O P A L ‘\
0’