Strings I-Computer Programming-Lecture Slides, Slides of Computer Programming

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

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal 🇮🇳

3

(1)

30 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 192
Lecture 11
Fall 2011
October 28-30, 2011
Ghufran Ahmed
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Strings I-Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

CS 192Lecture 11Fall 2011

October 28-30, 2011

Ghufran Ahmed

Strings

•^

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’,

‘\0’};

•^

cout<<a1;

cout<<a2;

cout<<a3;

•^

Is

a2[]

a string?

•^

char

b[4]=“hi”;

b[3]

set to zero (null) as is

b[2]

Reading Strings from Keyboard

•^

There is a problem with previous program, if the string haswhitespace characters

(space, tab, newline)

-^

Use gets() to read a string from the keyboard. #include <stdio.h>

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

==========================

strcpy(s1, s2)

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

==========================

strcat(s1, s2)

Appends

s

to the end of

s

and returns

s

.^ s

is unchanged

==========================

int main(){

char s1[20], s2[10];strcpy(s1, "hello");strcpy(s2, " there");strcat(s1, s2);cout << s1<<“\n”;return 0;

} //output?

String Library Functions^ ==========================

strmp(s1, s2)

•^

compares two strings

lexicographically

•^

returns 0 if they are equal (dictionary order)

•^

returns a +ve number if s1 is greater than s

•^

returns a -ve number if s1 is less than s

strcmp(“Zoo”, “ape”);

ASCII code has uppercase before lowercase

strcmp(“Zoo”, “zoo”);

// Output?

strcmp(s1, s2) …

bool password();int main(){

if ( password() )

cout << "Logged on.\n";

else

cout << "Access denied.\n";

return 0;

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

==========================

strlen(s)

returns the length of s

==========================

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

String/Numeric Conversion

n=atoi(s)

Converts

s

to integer

ln=atol(s)

Converts

s

to a long integer

fn=atof(s)

Converts

s

to a float

itoa(v,s,b)

Converts

v

integer of base

b

to a string

s

To Upper

char mystring[80] = "i am a string";int i=0;while ( mystring[i] ){^

// mystring[i] - ’a’ + ’A’

char upper = mystring[i] - (97 - 65);cout << upper;i++;

-^

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