Strings
An important point about array initialization
2
int a[] = {2,-1,’A’,2.15};
int a[] = {2,-1,65,2};
Yes ☺
But it is okay to assign values of different data types. I will convert all of them (if convertible) into the same data type (that of array)
I told you that array stores values of the same data type
Why?
ESC101: Fundamentals of Computing
Character Arrays and Strings
3
char str[50] = {‘H’,’e’,’l’,’l’,’o’,’ ‘,‘W’,’o’,’r’,’l’,’d’};
char str[50] = "Hello World";
char str[50] = {‘H’,’e’,’l’,’l’,’o’,’ ‘,‘W’,’o’,’r’,’l’,’d’,’\0’};
The null character
Note that not all 50 elements were initialized here (only first 11 were)
Equivalent to “Hello World”
ESC101: Fundamentals of Computing
The null character \0
4
char str[50] = {‘H’,’e’,’l’,’\0’,’l’,’o’,’ ‘,‘W’,’o’,’r’,’l’,’d’};
printf("%s",str);
Hmm … string is only till the \0. I will consider anything after that as garbage
Hel
Note: We use %s to print a string
ESC101: Fundamentals of Computing
Different ways to declare/initialize a string
5
char str[] = {‘H’,’e’,’l’,’l’,’o’,’ ‘,‘W’,’o’,’r’,’l’,’d’, ’\0’};
char str[] = "Hello World";
char str[50] = "Hello World";
You need not specify the size of string. But if you specify the size, it should be at least one more than the length of the string
char str[50] = {‘H’,’e’,’l’,’l’,’o’,’ ‘,‘W’,’o’,’r’,’l’,’d’, ’\0’};
char str[12] = {‘H’,’e’,’l’,’l’,’o’,’ ‘,‘W’,’o’,’r’,’l’,’d’, ’\0’};
char str[12] = "Hello World";
Note that Hello World has length 11, so size 12 is fine. Less than that may cause issues
ESC101: Fundamentals of Computing
C and the null character
6
char str[6] = "Nice";
str
N
i
c
e
\0
char str = "A";
putchar(str);
$
Strings are character arrays. “A” is a string. ‘A’ is a character
Somewhat like saying
int num = {3,2,1};
ESC101: Fundamentals of Computing
The C and the null character
7
str
N
i
c
e
\0
So
char str[6] = "Nice";
scanf("%s",str);
S
o
\0
printf("%s",str);
We did not write &str in scanf?
Will learn about this in a few weeks
No, since str is the whole array
The rest of the char array is still there
Yes, I did not erase ‘e’ and ‘\0’ that were already there. I just overwrote the first two characters and then put a \0
So
Will see it shortly
ESC101: Fundamentals of Computing
Strings/char arrays are very useful
bigNum[len – i] – ‘0’
8
a char
Len is the size of the string bigNum (can get it using strlen function
a char
Will see some functions today
ESC101: Fundamentals of Computing
Example: Adding two VERY BIG numbers
9
9343253466545736093899875874787574868
+ 43353672368646348598659693634909807
sum_rightmost_digit = bigNum1[len1-1] – ‘0’ + bigNum2[len2-1] – ‘0’;
sum_second_digit_from_right = bigNum1[len1-2] – ‘0’ + bigNum2[len2-2] – ‘0’
+ carry digit (if any) from rightmost
Keep going right to left by repeating this procedure (and store result as a string)….
Now ignore carry digit (if any) and add ‘0’ to get the char version of result
char ‘8’
char ‘7’
Suppose the sizes of the strings are len1 and len2, respectively (can get it using strlen function
Can store the result in another string/char array. Example:8+7 will give 15, ignoring carry 1, we have 5. To store 5 as a char, we can do ‘0’ + 5 which will give the character ‘5’
Add these rightmost digits first
Try writing the full program as a practice
ESC101: Fundamentals of Computing
scanf with Strings
10
scanf("%s",str);
Will discuss the reason in detail when we study Pointers
ESC101: Fundamentals of Computing
scanf with Strings: An Example
11
#include <stdio.h>
int main() {
char str1[20], str2[20];
scanf("%s",str1);
scanf("%s",str2);
printf("%s + %s\n", str1, str2);
return 0;
}
INPUT
IIT BHU
OUTPUT
IIT + BHU
INPUT
I am DON
OUTPUT
I + am
Read “I” as first string, stopped when saw white space and read “am” as second string, stopped again when saw the next space (“DON” ignored)
Not scared of you DON. I won’t read you ☺
ESC101: Fundamentals of Computing
gets with Strings
12
gets(str);
gets is deprecated in Clang
Do not use it regularly!
When some code becomes buggy or old or obsolete, it is declared as deprecated by the experts who developed that cod
No need for %s
ESC101: Fundamentals of Computing
getline with Strings
13
Syntax? We will see it when discussing Pointers
ESC101: Fundamentals of Computing
String and Substring
14
str
N
i
c
e
\0
o