Basic C program- C Strings

profilejeprej
hw_4_--_c-strings.pdf

Introduction to Programming CS1325 – Spring 2017

Assignment #4 – C-Strings

March 20th, 2017

Due Date: March 29th, 2017 – 10:00pm

Ask if you have questions, or need any clarification

Introduction

In this assignment you will write a small C program. Your program should compile correctly and

produce the specified output.

Please note that the computer program should comply with the commenting and formatting rules

as has been done in class. For example, there should be a header for the whole program that gives

the author’s name, class name, date, and description. End braces should be commented, and there

are alignment and indenting requirements as discussed. Please ask if you have any questions.

Program #1

Write a program to display the following menu:

1) Enter string

2) String length

3) Count characters case sensitive

4) Count characters non case sensitive

5) Validate string (Phone numbers)

6) Exit.

If user select a number from 2-5, a function should be called. All these functions should receive at

least one parameter. Option 3 and 4 must return an integer value, and display a table as shown

below, while option 5 must return 0 or 1 and display a message(if 0 “The string is NOT a valid

phone number”, and if 1 “The string is a valid phone number”). Option 6 will finish the program.

The string cannot be hard coded. A menu should be displayed (failing to display the menu will

translate to 0 as homework grade).

Given a string char str[](i.e. str[]= ”Programming Assignment”)

A) (10 points) Write a recursive function to find the length of the string.(option 2)

B) (40 points) Write a recursive function that returns how many different (not including repeated entries) characters (case sensitive) are in the string (i.e, ‘T’ is not the same as ‘t’).

Using this function display a table to indicate the number of different characters, and the

number of times each character appears in the string, for example

CS1325 – Introduction to Programming page: 2

Output:

Character Number Of Times

P 1

r 2

o 1

g 3

a 1

m 3

i 2

n 3

1

A 1

s 2

e 1

t 1

---------

Total: 13

C) (20 points)Write a function that returns how many different (not including repeated entries) characters are in the string. ‘T’ is the same as ‘t’, once you have either ‘T’ or ‘t’ it

should appear only once. Using this function display a table to indicate the number of

different characters, and the number of times each character appears in the string; for

example:

Output:

Character Number Of Times

P 1

R 2

O 1

G 3

A 2

M 3

I 2

N 3

1

S 2

E 1

T 1

Total: 12

D) (30 points)A function can be used to validate data entered. One example is to validate phone numbers. In that case, a string of fixed length (i.e. 10) and containing only numbers

are accepted. For example consider the input 9728834240. This string is valid because

we have only numbers, and the string length is 10. However 972883 is not valid, nor

ABC972883D. For example:

CS1325 – Introduction to Programming page: 3

Output:

9728834240 is a valid phone number!

ABC883424D is NOT valid phone number!

Deliverables C source code file. Make sure to include enough comments (or any other instruction) to execute

your program

The C source code file should:

1) Comply with all of the formatting requirements already discussed. 2) Display the output