You are on page 1of 54

LEARNING C

PROGRAMMING
COA1910

Strings in C
OUTLINE
• Pre-requisite for the lab
• Loops and Arrays
• What is Strings?
• Declaration of Strings
• String Initialization
• Reading from the string
• ASCII set value for character
Strings
• A string is a sequence of characters that is treated as a single data
item.
• Character strings are often used to build meaningful and readable
programs.
• Some common operations performed on character strings include:
• Reading and writing strings
• Combining strings together
• Copying one string to another
• Comparing strings for equality
• Extracting a portion of string
Declaring String Variables
• C does not support strings data type. However, it represents strings as
a character array.
• So, in C a string variable is any valid C variable name and is always
declared as an array of characters.
• General form of declaring a string variable
char string_name [size];
Declaring String Variables
• General form of declaring a string variable
char string_name [size];
• The size determines the number of characters in the string_name.
• Example,
char city[10]; or char name[30];
• However, when a compiler assigns a character string to a character
array it automatically adds a null character (‘\0’) at the end of the
string.
• Therefore, total size of the string is maximum characters plus one.
Initialization of character array
• Like numeric arrays, character arrays may be initialized when they are
declared.
• C permits a character array to be initialized in either of the following
two forms:
char city [9] = “ NEW YORK ”;
OR
char city [9] = {‘N’, ‘E’, ‘W’, ‘ ’, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’};
• It should be noted that when we initialize a character array by listing
its elements, we must supply explicitly the null terminator.
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
In this case computer creates a character array of size 6
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
Place the value GOOD in it
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
Place the value GOOD in it
G O O D
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
terminate it will null character
G O O D \0
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
Initialize all other elements with NULL.
G O O D \0
Initialization of character array
• C also permits us to initialize a character array without specifying the
number of elements.
• For example,
char str [ ] = {‘G’, ‘O’, ‘O’, ‘D’,‘\0’};
• We can also declare the size much larger than the string size in the
initializer.
char str [6] = “GOOD”
Initialize all other elements with NULL.
G O O D \0 \0
Reading strings from terminal
• The familiar input function scanf can be used with %s format specifier
to read a string of characters.
• Example:
char address[10];
scanf(“%s”, address);

• Lets see this with an working example


Reading strings from terminal
Reading strings from terminal

main() function
Reading strings from terminal

Declaration of a character array


name of size 20.
Reading strings from terminal

Output:
Reading strings from terminal

Output:
Reading strings from terminal

Output:
Reading strings from terminal

Output:

Now, lets try to read a name with a space in it.


Reading strings from terminal

Output:
Reading strings from terminal

Output:
Reading strings from terminal

Output:

The problem with the scanf function is that it terminates its input on
the first white space it finds. A white space includes blanks, tabs, and
new line.
Reading strings from terminal
• To overcome this issue of reading a complete string with white
spaces, we can use a intrinsic function getchar().
• This function can be used to read a single character from the
terminal.
• So, with the help of a loop we can use this function repeatedly to
read successive from the input and place them into a character array.
• The terminating condition in this case is the new line (‘\n’). Once this
is encountered we terminate the string with a NULL character (‘\0’).
Reading strings from terminal
Reading strings from terminal

Declaring a
character array
name and a
character variable
ch.
Reading strings from terminal

Here i is the loop


variable
Reading strings from terminal
In this while loop:
1. We check for the
terminating condition
i.e. character is new
line or not.
2. If YES, we exit the
while loop.
3. If NOT, we get inside
the loop and read the
single character using
getchar() function.
4. Move the character
into the character array
5. Increment the loop
variable.
Reading strings from terminal

Insert a null character at


the end of the character
array.
Reading strings from terminal
Reading strings from terminal
name[20]

ch
Reading strings from terminal
name[20]

ch
Reading strings from terminal
name[20]

ch

Output:
Reading strings from terminal
name[20]

ch

Output:
Reading strings from terminal
name[20]

ch
S

Output:
Reading strings from terminal
name[20]
S

ch
S

Output:
Reading strings from terminal
name[20]
S

ch
S

Output:
Reading strings from terminal
name[20]
S

ch
S

Output:
Reading strings from terminal
name[20]
S

ch
a

Output:
Reading strings from terminal
name[20]
S a

ch
a

Output:
Reading strings from terminal
name[20]
S a

ch
a

Output:
Reading strings from terminal
name[20]
S a

ch
a

Output:
Reading strings from terminal
name[20]
S a y

ch
y

Output:
Reading strings from terminal
name[20]
S a y y

ch
y

Output:
Reading strings from terminal
name[20]
S a y y

ch
e

Output:
Reading strings from terminal
name[20]
S a y y e

ch
e

Output:
Reading strings from terminal
name[20]
S a y y e
d U s M
a n

ch
\n

Output:
Reading strings from terminal
name[20]
S a y y e
d U s M
a n \0

ch
\n

Output:
Reading strings from terminal
name[20]
S a y y e
d U s M
a n \0

ch
\n

Output:

Output:
An easy way!!
• C supports a format specification known as edit set conversion code
%[…] that can be used to read a line containing a variety of
characters, including white spaces.
• It can use used in scanf function as follows:
scanf(“%[^\n]”, name); //read until new line char
Name Entered: New Delhi (Enter / New Line)
• The system will print:
print(“%s”, name); //The system will print New Delhi
ASCII Set Value of Character
• Whenever a character constant or character variable is used in an
expression, it is automatically converted into an integer value by the
system.
• Lets say, the system uses ASCII data set and we write the following
code:
x = ‘a’;

printf(“%d\n”, x);

• The system will display the number 97 (i.e. the ASCII value of small a)
on the screen.
ASCII Set Value of Character
• In ASCII character set, the decimal number 65 to 90 represent upper
case alphabets and 97 to 122 represents lower case alphabets.
Thanks

You might also like