Upload
adam-stanley
View
214
Download
0
Embed Size (px)
Citation preview
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]<<“ “;
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;
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”
Extraction Operator >> (string)
char word[4];
cout<<“ Please enter a 3-letter word”; // “cat”
cin >> word;
cout<<“You have entered :” ;
cout << word; // ”cat”
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”
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”
Extraction Operator >> (string)
char phrase[10];
cout<<“ Please enter a sentence”;// “This is hard”
cin >> phrase;
cout<<“You have entered :” ;
cout << phrase; // ”This”
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???
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
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);
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”
Initialization of Strings
char my_word[6] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’, ‘\0’};
char my_word[6]=“class”;
char my_word[ ]=“very interesting class”;
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
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;
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.
Worksheet
Write a function that will accept two strings and return true if they are equal in length and false if they are not.
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.