Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

UNIT

3 STRINGS

IT 105 Computer Programming 2


LESSON 3
Array of Strings

IT 105 Computer Programming 2


Array of Strings

A string is a one-dimensional array of characters.

char color [10] ;


//declares string variable color with a maximum size of 10
characters including the ‘\0’
An array of strings is a two-dimensional array of characters.

char color [3] [10] ;


//declares string variable color for three kinds of color with a
maximum size of 10 characters including the ‘\0’

IT 105 Computer Programming 2


Array of Strings

String
char color [10] = “Red”;

Array of Strings

char color [3] [10] = {“Red”, “Blue”, “Yellow”};

color [0] is Red , color[1] is Blue, color[2] is Yellow

IT 105 Computer Programming 2


Array of Strings

char color [3] [10] = {“Red”, “Blue”, “Yellow”};

color [0] R e d \0
color [1] B l u e \0
color [2] Y e l l o w \0

IT 105 Computer Programming 2


Array of Strings

char color [3] [10] = {“Red”, “Blue”, “Yellow”};

cout <<color[0]; Output : Red


//prints the first string

cout <<color[0][0]; Output : R


//prints the first character of the first string

IT 105 Computer Programming 2


Array of Strings
Sample Program
A program that reads name, course/section,subject and grade
of 3 students and displays if grade is passed or failed. Passing
grade is 75 and above.

IT 105 Computer Programming 2


Array of Strings

Variables
names of 3 students with maximum size of 20 each name including ‘\0’
studname[0] (first student ) studname[1] (second student )
studname[2] (third student )

course/sec of 3 students with maximum size of 15 including ‘\0’


coursec[0] (course/section of first student ) coursec[1] (course/section of second student )
coursec[2] (course/sec of third student )

subject name of 3 students with maximum size of 10 including ‘\0’


subj[0] (subject of first student ) subj[1] (subject of second student )
subj[2] (subject of third student )

grade of 3 students

grade remarks of 3 students with maximum size of 7 including ‘\0’


graderemarks[0] ( grade remarks of first student ) graderemarks[1] ( grade remarks of second student )
graderemarks[2] ( grade remarks of third student )

2 given grade remarks (PASSED or FAILED)


remarks[0] = “PASSED” , remarks [1]= “FAILED”

IT 105 Computer Programming 2


Array of Strings

variable i controls the no.of students

reading student name( studname[0],studname[1],studname[2])

reading course /section( coursec[0], coursec[1], coursec[2])

reading subject ( subj[0], subj[1], subj[2])

reading student grade ( grade[0], grade[1], grade[2])

testing each student grade if within the passing grade of 75 and above

copying string “PASSED”(remarks [0]) to graderemarks

copying string “FAILED”(remarks [1]) to graderemarks

used to skip/discard the enter key pressed after inputting student grade

IT 105 Computer Programming 2


Array of Strings

IT 105 Computer
IT 105 Computer Programming 2 Programming 2
Array of Strings

INPUT

OUTPUT

IT 105 Computer Programming 2


Array of Strings

without cin.ignore() , the enter


key pressed after entering
student grade will be placed on
the next input of student name

IT 105 Computer Programming 2

You might also like