You are on page 1of 34

CSE-105: Structured Programming Language

Shahriar Rahman Khan


Lecturer, Department of CSE, MIST
Email: shahriarkhan@cse.mist.ac.bd

1
Lec 9-11: String in C

PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
Character Arrays

» Character array - an array whose components are of type char


» A Character array is a sequence of the characters that are stored in consecutive
memory addresses.
» In a character array, a particular character can be accessed by its index.

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 3


C Strings (Character Arrays)

» String - a sequence of zero or more characters enclosed in double quote marks


» The last character in a string is the null character (‘\0’) that terminates the string.
» There is a difference between 'A' and "A"
» 'A' is the character A
» "A" is the string A
» Because strings are null terminated, "A" represents two characters, 'A' and '\0‘
» Similarly, "Hello" contains six characters, 'H', 'e', 'l', 'l', 'o', and '\0'

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 4


Character Array
» The most common use of the one-dimensional array in C programming is
character array
» Declaration of character array:
char array_name [array_size];

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 5


Initializing Character Array
» Suppose you want to store the word ‘hello’ in a character array. How will you do
that?
» Step 1: declare character array of size 5 (five characters)
» Store the first character ‘h’ in array index 0
» Store the other characters in the same way

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 6


Character Array Printing
» store the word ‘hello’ in a character array and print it

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 7


Character Array Printing
» store the word ‘hello’ in a character array and print it

#include <stdio.h>
int main()
{
char values[]= {'h', 'e', 'l', 'l', 'o'};
int i;
printf("\n");
for (i = 0; i<9; i++)
{
printf("%c ", values[i]);
}
return 0;
}

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 8


What is String?

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 9


String in C

» String is a sequence of characters that are treated as a single data item and
terminated by a null character '\0'.
» Remember that the C language does not support strings as a data type.
» A string is actually a one-dimensional array of characters in C language. These
are often used to create meaningful and readable programs.

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 10


String in C
» String is defined as a NULL terminated character array.
» One basic difference between character array and string is, string MUST be
terminated by a null character.

Character Array h e l l o

String h e l l o \0

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 11


Initializing String
» Suppose you want to store the word ‘hello’ in a string. How will you do that?
» Step 1: declare character array of size 6 (five characters + null)
» Store the first character ‘h’ in array index 0
» Store the other characters in the same way

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 12


Initializing String

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 13


String Input - Output
» Format specifier for string input output is : “%s”
» Take input of a string and print it
Note: The ‘&’ is not required in string input.
'&' is used to get the address of the variable. C
does not have a string type, String is just an
array of characters and an array variable stores
the address of the first index location.

Input: Output:
hello h e l l o \0
hello
Note: When you take input of a string, you do not have to put the null character
explicitly. The compiler does it itself.
11/21/2022 LEC SHAHRIAR RAHMAN KHAN 14
String Input - Output
#include <stdio.h>
int main()
{
char values[6];
int i;
scanf("%s",values);
printf("%s is the string and %d the base address\n", values, values);
printf("%d the base address\n", &values[0]);
return 0;
}

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 15


String Input - Output
» Suppose you want to take input of a sentence like “hello world” using scanf
statement, what you would do?
○ Declare a string of size 12
○ Take input using scanf
» Print the output

h e l l o w o r l d \0

What will be the output?

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 16


String Input - Output

What we did wrong in this code?

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 17


String Input - Output
» scanf terminates if it encounters any white space during taking input
» White space examples: space, tabs, carriage return, newline etc
» In the previous code, when we entered a space(“ “) after the word “hello” scanf
statement terminated, means it stopped taking any further input
» That’s why our output printed upto the word “hello”
» How to solve this problem?
» C programming provides i/o functions to allow taking input and printing output
with whitespace
○ gets() for taking input with whitespaces
○ puts() for printing output with whitespaces
○ edit set conversion code %[..] that can be used to read a line containing a
variety of characters, including white spaces.

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 18


String Input - Output

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 19


Accessing Elements in a String
Consider the following String:
h e l l o w o r l d \0
❏ The first character ‘h’ is in Index 0
❏ The last character ‘d’ is in index 10

❏ You can access any element at any index just like a character array

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 20


String Problems
» Input a string and print it.
» Find the length of a string without using library function.
» Print individual characters of string in reverse order.
» Count the total number of words in a string.
» Compare two strings
» Take two strings as input and print out the concatenation of the input strings.
» Count total number of vowel or consonant in a string.
» Find maximum occurring character in a string.
» Sort a string array in ascending order.
» Read a string through keyboard and sort it using bubble sort.
» Extract a substring from a given string.
» Check whether a given substring is present in the given string.
» Read a sentence and replace lowercase characters by uppercase and vice-versa.

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 21


String Example 1
Take input of a string and calculate its length

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 22


String Example 2
Take input of a sentence and calculate the number of words in it [Assume there is
no space before the first word, every other word is separated by a single space]

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 23


String Example 3
Take two strings as input and copy the elements of string 2 in string 1.

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 24


String Example 4
Take two strings as input and print out the concatenation of the input strings

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 25


String Handling Functions
C/C++ have multiple built in function to make your life easier. There is a library named
<string.h>, which needs to be included in your c program to use these functions.

Function Description
strlen(string_name) Can compute the length of the string
Can copy the content of a string to
Strcpy(string1 , string2 )
another
Is used to concatenate or join two
Strcat(dest, src)
strings
Strcmp(string1 , string2) Can compare two strings
Strlwr(string_name) Can convert the string to lowercase
Is used to convert the letters of string
Strupr(string_name)
to uppercase
Strrev(string_name) Is used to reverse the string

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 26


strlen() Function

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 27


strcpy() Function

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 28


strcmp() Function
strcmp( string1, string2 ) //compares both string and returns:

03/11/2021 Topic: String in C 29


strcmp() Function
strcmp( string1, string2 ) //compares both string and returns:

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 30


strcmp() Function

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 31


strcat() Function

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 32


strrev() Function

11/21/2022 LEC SHAHRIAR RAHMAN KHAN 33


GOOD NEWS!
T H E CL A S S
I S O V E R…
THANK YOU!

11/21/2022 37

You might also like