CS 2073 Midterm 2 Review: Reversing and Checking Palindromes, Exams of Computer Science

A review for midterm 2 of cs 2073, focusing on reversing strings and checking if they are palindromes. It includes instructions for using #include files, defining constants, declaring arrays, and using string manipulation functions. The sample problem involves reading a string from the keyboard, reversing it, printing the reversed string, and comparing it to the original to determine if it is a palindrome.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-ogv
koofers-user-ogv 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2073 Review for Midterm 2
Basic knowledge
Use #include files.
Use #define to define a constant.
Declare an array (any type, any size).
Random Number Generator
Seed using srand()
Call rand_float(a.b) to return a double in the range [a,b)
Write rand_float() in advance and bring it to the test on your diskette.
Numerical arrays
Compute a numerical value expressed in summation notation. Examples:
or
Strings and character arrays
Use strlen(), strncmp(), and strncpy()
Process or analyze a character string. Examples:
How many vowels are in the string?
Print the string in reverse order.
Is the string a palindrome (eg., noon, mom, dad, 1441)?
Sorting
Sort an array of any type.
Write the sorting functions in advance and bring them to the test on your diskette.
Sample question:
Write the program described below. Do all work on the hard drive. When you are done, copy only the source
code (’C’code) onto your diskette and turn it in. Be sure your diskette is labeled.
Program: reverse.c
1. Include files <stdio.h>, <stdlib.h>, and <string.h>
2. Use a #define statement to define the constant MAXLINE to be 256.
3. Declare 2 character arrays, each of size MAXLINE.
4. Read in a character string from the keyboard into one array.
5. Reverse the string, saving it in the second array. Print the reversed string.
6. Compare the original and reversed strings to see if it is a palindrome, ie., the same forwards and backwards.
N
i
i
x
N
x
1
_
1
N
i
i
xx
N
1
2
)(
1
pf2

Partial preview of the text

Download CS 2073 Midterm 2 Review: Reversing and Checking Palindromes and more Exams Computer Science in PDF only on Docsity!

CS 2073 Review for Midterm 2

Basic knowledge

Use #include files.

Use #define to define a constant.

Declare an array (any type, any size).

Random Number Generator

Seed using srand()

Call rand_float(a.b) to return a double in the range [a,b)

Write rand_float() in advance and bring it to the test on your diskette.

Numerical arrays

Compute a numerical value expressed in summation notation. Examples:

or

Strings and character arrays

Use strlen(), strncmp(), and strncpy()

Process or analyze a character string. Examples:

 How many vowels are in the string?

 Print the string in reverse order.

 Is the string a palindrome (eg., noon, mom, dad, 1441)?

Sorting

Sort an array of any type.

Write the sorting functions in advance and bring them to the test on your diskette.

Sample question:

Write the program described below. Do all work on the hard drive. When you are done, copy only the source

code (’C’code) onto your diskette and turn it in. Be sure your diskette is labeled.

Program: reverse.c

1. Include files <stdio.h>, <stdlib.h>, and <string.h>

2. Use a #define statement to define the constant MAXLINE to be 256.

3. Declare 2 character arrays, each of size MAXLINE.

4. Read in a character string from the keyboard into one array.

5. Reverse the string, saving it in the second array. Print the reversed string.

6. Compare the original and reversed strings to see if it is a palindrome, ie., the same forwards and backwards.

N i

x i

N

x

1

_ 1

N i

x x i

N 1

^1 ( )^2

  • reverse.c
  • CS 2073 Midterm 2: Sample problem
  • Reads in a strings from the keyboard.
  • Prints string in reverse order.
  • Compares original string to reversed string to see if it is a palindrome.
  • S. Dykes
  • 4-8-
  • SAMPLE OUTPUT FROM VISUAL C++
  • Enter string: highnoon
  • Reverse string: noonhgih
  • Not a palindrome ------------------------------------------------------------------------------/ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINE 256 int main(void) { char str[MAXLINE], rev[MAXLINE]; int i, length; /*--------------------------------------------
  • Read string and determine length --------------------------------------------/ printf("Enter string: "); scanf ("%s", str); length= strlen(str); /*---------------------------------------------
  • Build the reversed string and print it.
  • Don't forget to add the terminating 0! ---------------------------------------------/ for (i=0; i<length; i++) rev[length-i-1] = str[i]; rev[length] = 0; printf("Reverse string: %s\n\n", rev); /*---------------------------
  • Palindrome? ---------------------------/ if (strcmp (str,rev) == 0) printf ("Palindrome!\n\n"); else printf ("Not a palindrome\n\n"); return 0; }