Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3758478 • Letter: I

Question

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

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

A) Write a recursive function to find the length of the string.

B) 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:

C) 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

D) A function can be used to validate data entered. One example is to validate phone numbers. In that case, a fixed length (i.e. 10) and only numbers are accepted. For example consider the input 9728834240. This is string is valid because we have only numbers, and the string length is 10. However 972883 is not valid, nor ABC972883D. For example:

Output:

9728834240 is a valid phone number!

ABC883424D is NOT valid phone number!

The string cannot be hard coded. A menu should be displayed (failing to display the menu will translate to 0 as homework grade):

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 above, while option 5 must return 0 or 1 and display a message. Option 6 will finish the program.

Output: Chara cter Number Of Times 1 Total: 13

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void ReadStr(char str[50])
{
printf("Enter the string: ");
gets(str);
}

void countCharsCaseSensitive(char str[50])
{
int i, j, repeated, count;
char temp;
for(int i = 0; i < strlen(str); i++)
{
repeated = 0;
temp = str[i];
for(j = i-1; j >= 0; j--)
if(str[j] == temp)
repeated = 1;
if(!repeated)
{
count = 1;
temp = str[i];
for(j = i+1; j < strlen(str); j++)
if(str[j] == temp)
count++;
printf("%c %i ",temp,count);
}   
}
}

void countCharsNonCaseSensitive(char str[50])
{
char temp[50];
int i;
strcpy(temp, str);
for(i = 0; i < strlen(str); i++)
temp[i] = toupper(str[i]);
temp[i] = '';
countCharsCaseSensitive(temp);
}
  
void validateString(char str[50])
{
int valid = 1, i;
puts(str);
if(strlen(str) != 10)
valid = 0;
for(i = 0; i < strlen(str); i++)
if(!isdigit(str[i]))
valid = 0;
if(valid == 1)
printf("The entered string is valid. ");
else
printf("The entered string is NOT valid. ");   
}
int main()
{
char str[50] = "";
int choice;
while(1)
{
printf("1) Enter string 2) String length 3) Count characters case sensitive ");
printf("4) Count characters non case sensitive 5) Validate string (Phone numbers) 6) Exit. ");
printf("Enter your choice: ");
scanf("%i", & choice);
switch(choice)
{
case 1: ReadStr(str); break;
case 2: printf("The length of the string is: %i ",(int)strlen(str)); break;
case 3: countCharsCaseSensitive(str); break;
case 4: countCharsNonCaseSensitive(str); break;
case 5: printf("Enter the phone number: "); scanf("%s",str); validateString(str); break;
case 6: exit(0);
default: printf("Invalid menu choice. Try again. ");
}
}
}