


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: Items, Strings, Double, Quotes, Strings, Character, Array, Keyboard, Whitespace, Library, Function
Typology: Slides
1 / 34
This page cannot be seen from the preview
Don't miss anything!



























A collection of items of same type in contiguous memory
-^
One-dimensional array of type
char
e.g.
”hello”
A string is in double quotes and is called literal string constant
-^
A string is terminated by the null character: ‘\0’ (a zero)
-^
Compiler automatically appends it at the end of string; signifiesend of string for the compiler
-^
char
str[6]
“hello”;
str[5]
is what?
char
a1[]=“abc”;
char
a2[]={‘a’,
‘b’,
‘c’};
char
a3[]={‘a’,
‘b’,
‘c’,
cout<<a1;
cout<<a2;
cout<<a3;
Is
a2[]
a string?
char
b[4]=“hi”;
b[3]
set to zero (null) as is
b[2]
-^
int main(){
char str[80];cout << "Enter a string: ";cout.flush();gets(str); // read a string from the keyboardcout << "Here is your string: ";cout << str<<“\n”;return 0;
}
/*
The
gets
function reads a line,
which consists of all characters up to andincluding the first newline character ('\n'). gets
then replaces the newline character
with a null character ('\0') before returningthe line. */
gets() Library Function
String Library Functions
==========================
Copies until \0 is moved. Contents of
s
overwritten
s
should have enough space. The value of
s
is returned
==========================
int main(){
char str[80];strcpy(str, "hello");cout << str;return 0;
}^
Output is: hello
String Library Functions
==========================
==========================
char s1[20], s2[10];strcpy(s1, "hello");strcpy(s2, " there");strcat(s1, s2);cout << s1<<“\n”;return 0;
strcmp(s1, s2) …
int main(){
char s[80];for(;;){
cout << "Enter a string: ";cout.flush();gets(s);if( !strcmp("quit", s) )
break;
} return 0;
}
strcmp(s1, s2) …
int main(){
char s[80]; do{ cout << "Enter a string:";gets(s);} while(strcmp("quit",s));
//other than 0 is true
return 0; }
String Library Functions
==========================
==========================
int main(){
char str[80];cout << "Enter a string: ";gets(str);cout << "Length is: " << strlen(str) <<"\n";return 0;
} //what is the output if we enter: this is cs
-^
Test condition of the for loop is simply thearray indexed by i
-^
Loop runs until it encounters the nullterminator.==================================
toupper()
tolower()
isalpha()
isdigit()
isspace()
ispunct()
Using Null Terminator
-^
The following declares an array of 30 strings
-^
Each having a maximum length of 80 characters
char str_array[30][80];
-^
In order to access an individual string, specifyonly the left index.
Arrays of Strings