Strings: Divyashikha Sethia (DTU) Divyashikha@dtu - Ac.in
Strings: Divyashikha Sethia (DTU) Divyashikha@dtu - Ac.in
Divyashikha Sethia
(DTU)
[email protected]
4.1 STRINGS
•In C, a string is a null-terminated character array.
•After the last character, a null character ('\0') is stored to
signify the end of the character array.
Following is an example of declaring an array that has
five characters,
char str[] = "HELLO";
2
STORAGE
Name of character array (or the string): pointer to the
beginning of the string.
3
ACCESS TO
CHARACTERS
•Similar to subscripts/index to
access the elements of an array,
use subscripts to
access the elements of a string.
•Start subscript: 0 All the characters
of a string are stored in successive
memory locations.
•String is a sequence of characters.
•Character H is stored at memory
location 1000 but in reality, the
ASCII code of a character is stored
in the memory and not the
character itself. 4
DECLARATIONS
Constant string
char str[] = "HELLO";
Illegal declaration
char str[3];
str = "HELLO";
=> The main pitfall using this function is that the function terminates as
soon as it finds a blank space. For example, if the user enters Hello
World, then the str will contain only Hello .
•gets() function
gets(str);
The gets() function takes the starting address of the string which will
hold the input. The string inputted using gets() is automatically
terminated with a null character. It doesn‟t suffer from the drawback of
8
scanf() function
READING STRINGS
•getchar()/getch()/getche() function
Strings can also be read by calling the getchar() function
repeatedly to read a sequence of single characters
(unless a terminating character is entered) and
simultaneously storing it in a character array as shown
below.
i=0;()
ch = getchar();// Get a character
while(ch != '*') // ‘*’ is the terminating character here
{
str[i] = ch;// Store the read character in str i++;
ch = getchar();// Get another character
}
str[i] = '\0';// Terminate str with null character
We may also use width and precision specifications along with %s.
The width specifies the minimum output field width. If the string is
short, the extra space is either left padded or right padded. A
negative width left pads short string rather than the default right
justification.
The above statement would print only the first three characters in a
total field of five characters. Also these characters would be right
justified in the allocated width. To make the stringleft justified, we
10
must use a minus sign.
WRITING STRINGS
puts() function
puts(str);
11
4.2 OPERATION ON
STRINGS
•Finding Length of a String
•Converting Characters of a String into Upper/ Lower
Case
•Appending a String to Another String
•Comparing Two Strings
•Reversing a String
•Extracting a Substring from a String
•Inserting a String in the Main String
•Pattern Matching
•Deleting a Substring from the Main String
•Replacing a Pattern with Another Pattern in a String 12
LENGTH OF STRING
•The number of characters in a string
constitutes the length of the string.
•For example, LENGTH("C
PROGRAMMING IS FUN") will return
20. Note that even blank spaces are
counted as characters in the string.
•In the algorithm, Iis used as an index
for traversing string STR. To traverse
each and every character of STR, we
increment the value ofI.
•Once we encounter the null character,
the control jumps out of the while loop
and the length is initialized with the
value ofI.
•The library functions trlen(s1) which
is defined in
• string.hreturns the length of string 13
s1.
STRING TO
UPPERCASE/LOWERCASE
•The ASCII code forA–Zvaries from 65 to 91 and the ASCII
code fora–zranges from 97 to 123.
•So, if we have to convert a lower case character into
• uppercase, we just need to subtract 32 from the ASCII
• value. And if we have to convert character into lower
• case, we need to add 32 to the ASCII value.
•In the algorithm, using Ias the index of STR, we traverse
each character of STR from Step 2 to 3.
•If the character is in lower case, then it is converted into
• upper case by subtracting 32 from its ASCII value.
Otherwise, it is copied into the UPPER STRstring.
•Finally, when all the characters have been traversed, a null
character is appended toUPPER STR.
•The library functionstoupper()andtolower()which are defined
inctype.hconvert a character into upper and lower case,
14
respectively.
APPENDING ONE STRING
TO ANOTHER
•Appending one string to another string involves copying the contents of
the source string at the end of the destination string.
•So, if S1 is the source string and S2 is the destination string, then
appending operation would leave the source string S1 unchanged and
the destination string S2 = S2 + S1.
•The library function strcat(s1, s2)which is defined in string.h
concatenates string s2 to s1.
•In the algorithm, we first traverse through
the destination string to reach its end.
•The characters of the source string are then
copied into the destination string starting
from that position.
•Finally, a null character is added to
terminate the destination string.
15
COMPARING TWO
STRINGS
•If S1 and S2 are two strings, then comparing the two strings
will give either of the following results:
•In Step 2, a while loop is executed until all the characters of the
string are accessed.
•In Step 3, we swap the ith character of STR with its jth character.
As a result, the first character of STR will be replaced with its
last character, the second character will be replaced with the
second last character of STR, and so on.
•In Step 4, the value of I is incremented and J is decremented to
traverse STR in the forward and backward directions,18
respectively.
EXTRACTING SUBSTRING
FROM A STRING
•To extract a substring from a given string, we need the
following three parameters:
1. the main string,
2. the position of the first character of the substring in
the given string, and
3. the maximum number of characters/length of the
substring.
•For example, if we have a string
str[] = "Welcome to the world of programming";
•Then, result should be :
SUBSTRING(str, 15, 5) = world
19
EXTRACTING SUBSTRING
FROM A STRING
•In this algorithm, we initialize a
loop counter Ito M, that is, the
position from which the characters
have to be copied.
•Steps 3 to 6 are repeated until N
characters have been copied.
•With every character copied, we
decrement the value of N.
•The characters of the string are copied into
another string called the SUBSTR.
•At the end, a null character is appended to
SUBSTR to terminate the string.
20
INSERTING A STRING IN MAIN STRING
•The insertion operation inserts a string S in
the main text T at the kth position.
•The general syntax of this operation is
I NSERT(text, position, string).
For example, INSERT("XYZXYZ", 3,
"AAA")="XYZAAAXYZ"
•The algorithm first initializes the indices into
the string to zero.
•From Steps 3 to 5, the contents
ofNEW_STRare built.
•IfIis exactly equal to the position at which the
substring has to be inserted, then the inner
loop copies the contents of the substring into
NEW_STR.
•Otherwise, the contents of the text are copied
into it.
21
PATTERN MATCHING
•This operation returns
the position in the string
where the string pattern
first occurs. For
example,
char str[] = "Welcome to the world of
programming";
char pattern[] = "world";
INDEX(str, pattern); // = 15
22
PATTERN MATCHING
•Inthis algorithm, MAX is
initialized to
• length(TEXT)–Length(STR) + 1.
•For example, if a text contains 'Welcome
To Programming‘ and the string contains‘
World', in the main text, we will look for at
the most 22–5
• + 1 = 18 characters because after that
there is no scope left for the string to be
present in the text.
•In the inner loop in Step 3, we check the n
characters of string with the n characters of
text to find if the characters are same.
•If it is not the case, then we move to
Step 6,
• where I is incremented.
23
PATTERN MATCHING
•If the string is found, then the index
is initialized with I, else it is set to–
1.
•For example, if
•TEXT =“WELCOMETO THEWORLD”
•STRING =“COME”
•In the first pass of the inner loop,
we will compare “COME” with
“WELC” character by character.
•As„W‟and„C‟do not match, the
control will move to Step 6 and
then “ELCO” will be compared with
“COME”. In the fourth pass,
“COME”will be compared with
“COME”.
24
DELETING A SUBSTRING
FROM MAIN STRING
The replacement operation is used to replace the pattern P1
by another pattern P2.
Syntax : REPLACE(text, pattern1, pattern2)
For example,
REPLACE("AAABBBCCC", "BBB", "X"); // = AAAXCCC
REPLACE("AAABBBCCC", "X", "YYY"); // = AAABBBCC , no change since pattern 1 not found in text.
•In the algorithm, we first find the position POS, at which the
pattern occurs in the text.
•Then delete the existing pattern from that position and insert
a new pattern there.
25
REPLACING A PATTERN WITH
ANOTHER PATTERN IN THE
STRING
•The replacement operation is used to replace the pattern P1
by another pattern P2.
Syntax : REPLACE(text, pattern1, pattern2)
For example,
REPLACE("AAABBBCCC", "BBB", "X"); // = AAAXCCC
REPLACE("AAABBBCCC", "X", "YYY"); // = AAABBBCC , no change since pattern 1 not found in
text.
•In the algorithm, we first find the position POS, at which the
pattern occurs in the text.
•Then delete the existing pattern from that position and insert
a new pattern there.
26
4.3 ARRAY OF STRINGS
An array of strings is declared as
char names[20][30]; // names of 20 students in a class
• Here, the first index will specify how many strings are
needed and the second index will specify the length of every
individual string. So here, we will allocate space for 20 names
where each name can be a maximum 30 characters long.
• If we have an array declared as
char name[5][10] = {"Ram", "Mohan", "Shyam", "Hari", "Gopal"};
27
ARRAY OF STRINGS
By declaring the array names
char name[5][10] = {"Ram", "Mohan", "Shyam", "Hari", "Gopal"}; // 4 + 6 + 6 + 5 + 6 = 27 bytes
29
POINTERS AND STRINGS
• Instead of using the while loop, we could straightaway use the
function puts(), as shown below
puts(pstr);
31