18
Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Embed Size (px)

Citation preview

Page 1: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Arrays

Character Arrays and Strings

Alina Solovyova-Vincent

Department of Computer Science & Engineering

University of Nevada, Reno

Fall 2005

Page 2: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet

char my_word[5] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’}; Is it a character array or a string? Why? How can you display it to the screen?

for(i=0; i<5; i++)

cout<< my_word[i]<<“ “;

Page 3: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet

char my_word[6] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’, ‘\0’}; Is it a character array or a string? Why? How can you display it to the screen?

cout<< my_word;

Page 4: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Extraction Operator >> (char array)

char word[3];

cout<<“ Please enter a 3-letter word”; // “cat”

for ( int i=0; i < 3; i++)

cin >> word[i];

cout<<“You have entered :” ;

for ( i=0; i < 3; i++)

cout << word[i]; // ”cat”

Page 5: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Extraction Operator >> (string)

char word[4];

cout<<“ Please enter a 3-letter word”; // “cat”

cin >> word;

cout<<“You have entered :” ;

cout << word; // ”cat”

Page 6: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Extraction Operator >> (string)

char word[4];cout<<“ Please enter a 3-letter word”; // “cat”for ( int i=0; i < 3; i++) cin >> word[i];

word[3]=‘\0’; //convert character array into string

cout<<“You have entered :” ; cout << word; // ”cat”

Page 7: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Extraction Operator >> (char array)

char phrase[10];

cout<<“ Please enter a sentence”;// “This is hard”

for ( int i=0; i < 10; i++)

cin >> phrase[i];

cout<<“You have entered :” ;

for ( i=0; i < 10; i++)

cout << phrase[i]; // ”Thisishard”

Page 8: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Extraction Operator >> (string)

char phrase[10];

cout<<“ Please enter a sentence”;// “This is hard”

cin >> phrase;

cout<<“You have entered :” ;

cout << phrase; // ”This”

Page 9: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

getline( )

char my_string[10];

cin.getline(my_string, 10); //user enters hi

my_string[0]=‘h’

my_string[1]=‘i’

my_string[2]=‘\0’

my_string[3]= garbage

my_string[9]= garbage

cout<<my_string;

Displayed: hi

What is the length of this string???

Page 10: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

getline( )

char my_string[10];

cin.getline(my_string, 10); //user enters “I am happy”

my_string[0]=‘I’

my_string[1]=‘ ’

my_string[2]=‘a’

my_string[3]=‘m’

my_string[8]=‘p’

my_string[9]=‘\0’

cout<<my_string;

Displayed: I am happ

Page 11: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Function get( ) - page 104

cin.get( )

reads in a single character at a time. Will read any character.

if you need to read the ENTER key, a space, or the tab key, you cannot use cin. You must use cin.get because cin.get will read any character.

Example: char c1; cin.get(c1);

Page 12: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Function get( )

char phrase[12];

cout<<“ Please enter a sentence”;// “This is hard”

for ( int i=0; i < 12; i++)

cin.get(phrase[i]);

cout<<“You have entered :” ;

for ( i=0; i < 12; i++)

cout << phrase[i]; // ”This is hard”

Page 13: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Initialization of Strings

char my_word[6] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’, ‘\0’};

char my_word[6]=“class”;

char my_word[ ]=“very interesting class”;

Page 14: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet

Write a code segment that will prompt the user for his first name and his last name and display to the screen:

“Hello, first_name last_name”.

I.e. If user enters John Smith, the program should display

Hello, John Smith

Page 15: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

char first_name[20], last_name[20];

cout<<”\nEnter your first and last name ”;

cin>>first_name >> last_name;

cout<<”\nHello, “<< first_name <<” “<<last_name;

OR

char name[50];

cout<<”\nEnter your first and last name ”;

cin.getline(name, 50);

cout<<”\nHello, “<< name;

Page 16: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet

Write a code segment that will prompt the user for his first name and his last name and display to the screen:

“Hello, last_name first_name”.

I.e. If user enters John Smith, the program should display

Hello, Smith John.

Page 17: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet

Write a function that will accept two strings and return true if they are equal in length and false if they are not.

Page 18: Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet

Write a function that will accept an integer array, its size and a “code” integer. The function should find the location of that code integer in the array and return its position (index) in the array or 999 if the code was not found in the array.