Practical_09_Strings.docx - Of Programming Practical 9 refer to Topics17 and 18 Part A(Understanding Concepts 1 Given the program below answer the

Practical_09_Strings.docx - Of Programming Practical 9...

This preview shows page 1 out of 8 pages.

You've reached the end of your free preview.

Want to read all 8 pages?

Unformatted text preview: UECS1643Fundamentals Of Programming Practical 9 – refer to Topics17 and 18 Part A(Understanding Concepts) 1. Given the program below, answer the following questions. (a) What is the output if the input is the following? i) 5 ii) P iii) (Tab key) iv) g v) ? (b) Why code ch = getchar();is usedinstead ofcin>>ch; ? 1 UECS1643Fundamentals Of Programming 2. Given the program below, answer the following questions. (a) What is the output if the input is the following? i) h ii) Y iii) 3 (b) What will be the output if a non-alphabet character is sent to function tolower() or toupper() ? 3. The following program displays a table showing the characters from a toz together with their ASCII codes. (a) Run the program and observe the output. 2 UECS1643Fundamentals Of Programming (b) Modify the code above to get the following output: 4. Given the following program: 3 UECS1643Fundamentals Of Programming What is the output? 5. Given the declarations below, what is the output of the following statements? (a) cout<<strcmp(s1, s2) <<endl; (b)cout<<strcmp(s2, s3) <<endl; (c) cout<<strncmp(s1, s3, 4) <<endl; 6. What is the output of the following program fragment? 4 UECS1643Fundamentals Of Programming 7. The following program asks the user to enter a word. The program will display the word backward. It is assumed that the word will not exceed 20 characters.The program displays the characters one by one starting from the last character. Answer the following questions: 5 UECS1643Fundamentals Of Programming 8. #include<iostream> #include <iostream> #include int table[6][6], row, col; char<iostream> s1[10] = "good"; W using namespace #include #include<cstring> <cctype> <cstring> #include std; // char for character string <cctype> manipulation manipulation // for character functions manipulation functions s2[10] = "goose";functions using namespaceusing usingnamespacestd; std;char namespace for (row std; = 0; row < 6; row++) s3[10] = "goodies"; int main(void) for (col = 0; col < 6; col++) { main(void) int main(void) int if (row == col) { charch; { table[row][col] = 0; charchIn, char s1[15], word[21]; chOut; s2[5]; charch; else if (row > col) cout<< inti, len;;"Letter\tASCII Code\n"; table[row][col] = -1; for strcpy(s1, (ch ="Enter 'a'; "good"); cha<= 'z'; ch++) else cout<< cout<< character: "Enter "; a character: "; strcpy(s2, cout<<"Enter cout<< " job"); a"word: <<ch<< "; "\t " << (int)ch<<endl; table[row][col] = 1; cin>>chIn; ch" = getchar(); return 0; cin>> word; for (row = 0; row < 6; row++) } chOut = "s1 cout<< tolower(chIn); length: if (isalnum(ch)) "{ <<strlen(s1) <<endl; cout<<"The word backwards: for (col = "; 0; col < 6; col++) cout<< "s2 "tolower length: {ofprinted " <<strlen(s2) <<chIn<< " returns <<endl; " <<chOut<<endl; len = strlen(word); cout<<setw(3) << table[row][col]; chOut = toupper(chIn); cout<< "Alphanumeric character\n"; for (i = len 1; i>= 0; i--) cout<<endl; cout<< "s1: "toupper " <<of s1" <<chIn<< <<endl; if (isalpha(ch)) " returns " <<chOut<<endl; word[i]; cout<<cout<< "s2 from 2nd}element: { " <<&s2[1] <<endl; cout<<endl; strcat(s1, if (islower(ch)) return 0; s2); return cout<<0;"s1: " << s1 <<endl; cout<< "Lowercase letter\n"; else if (isupper(ch)) } strncat(s1, s2, 3); cout<< "Uppercase letter\n"; } cout<< "s1: " << s1 <<endl; } return 0; else if (isdigit(ch)) (a) Why header file <cstring> } is included? cout<< "Digit\n"; } else if (isspace(ch)) cout<< "Whitespace\n"; (b) What is the output if the word entered is else “program” ? cout<< "Some other character\n"; return 0; (c) What will be the output if you enter “Physics } program” ? (d) The program could not work if user enters more than one word separated by whitespaces. Modify the code above so that the program will work if user enters a word or a sentence (words separated by whitespaces). Assume the sentence will not exceed 50 characters. Note: Use function getline to input the sentence and store in an array. rite statements for the following: 6 UECS1643Fundamentals Of Programming (a) Declare an array named table with 5 rows and 3 columns to store 15 integers. (b) Assign all the elements to the value 0 using nested loops. (c) Assign the value 1 to all the elements in the first row using a loop. (d) Assign the value -1 to all the elements in the last column using a loop. (e) Compute and display the total of all the elements using nested loops. Part B (Programming Exercises) 1. Write a program that accepts a character from the user and displays the next character in the alphabet. Your program should only do this if the input is an alphabetic character. If the character entered is z (or Z), display a (or A). 2. Write a program that asks the user to enter a word and displays the word, character by character, with all the letters converted to uppercase.Assume the word will not exceed 20 characters. 3. Write a function that receives a string and counts and returns the number of characters in the string. Do not use any built-in library functions. 4. Write and test a function named countUpper that has a string parameter. The function counts and returns the number of uppercase letters in the string. 5. Write and test a function named searchCharthat has 2 parameters: a string (i.e. an array of characters)and a single character. The function counts and returns the number of times the character appears in the string. 6. Write and test a function that has a two dimensional array and its number of rows as parameters. The function computes and returns the largest of all the elements of the array. Assume the array has 4 columns. 7. Write and test a function that has a two dimensional array and its number of rows as parameters. The function computes and returns the smallest of all the elements of the array. Assume the array has 4 columns. 7 UECS1643Fundamentals Of Programming Part C (Self-Review / Revision) 1. What is the purpose of each of the following character manipulation functions? isalpha, isdigit, isalnum, isupper, islower, isspace, toupper, tolower. 2. How is a character stored in the computer? 3. How is a string stored in the computer? 4. What is the difference between reading a string using cin and cin.getline? 5. What is the purpose of each of the following string manipulation functions? strlen, strcpy, strncpy, strcmp, strcat, strncat. 6. What is the general format to declare a two-dimensional array? 7. How do you access an element of a two-dimensional array? Part D (Practice Exercises) 1. Write a program that accepts a letter and displays the position of the letter in the alphabet. For example, letter a has position 1, b position 2, and so on. The program should be able to handle both upper- and lowercase letters. If the character entered is not alphabetic, the program should display a message and stop. Note: The program should convert the letter to lowercase case first before determining the position. 2. Write and test a function named convertCasethat has an array of charactersas a parameter. The function converts all the letters in the word to uppercase letters. 3. Write and test a function that has a two dimensional array and its number of rows as parameters. The function computes and returns the total of all the elements of the array. Assume the array has 4 columns. 4. Write and test a function reverseWord that has a string parameter. The function reverses the letters in the string. The program should perform the following to exchange the first and last characters in the string: (i) Copy the first character of the string to a temporary variable. 0 1 2 3 4 5 6 7 8 g o o g l e \0 ? ? g temp g temp (ii) Copy the last character to the first position in the string. 0 1 2 3 4 5 6 7 8 e o o g l e \0 ? ? (iii) Copy the character in the temporary variable to the last character position in the string. 0 1 2 3 4 5 6 7 8 e o o g l g \0 ? ? g temp Repeat the steps to exchange the second and second-last characters, third and third-last characters, and so on. 8 ...
View Full Document

  • Summer '16
  • Mr.Chan
  • Energy, ASCII, cout

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture