Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005."— Presentation transcript:

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

2 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]<<“ “;

3 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;

4 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”

5 Extraction Operator >> (string) char word[4]; cout<<“ Please enter a 3-letter word”; // “cat” cin >> word; cout<<“You have entered :” ; cout << word; // ”cat”

6 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”

7 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”

8 Extraction Operator >> (string) char phrase[10]; cout<<“ Please enter a sentence”;// “This is hard” cin >> phrase; cout<<“You have entered :” ; cout << phrase; // ”This”

9 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???

10 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

11 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);

12 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”

13 Initialization of Strings char my_word[6] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’, ‘\0’}; char my_word[6]=“class”; char my_word[ ]=“very interesting class”;

14 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

15 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;

16 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.

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

18 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.


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

Similar presentations


Ads by Google