I'm very new to C programming, but I really need help with this assignment and the tutoring lab at my school wasn't much help. I need to write several functions in C to work with a switch statement to manipulate strings. There needs to be one to return the character in the string with the largest ASCII value, one to count the number of uppercase letters, one to double the string, one to replace the character at a certain index, one to insert a character at a certain index and one to print the string. I've started all of them (with the exception of the insert character...I don't know where to begin), but I'm not familiar enough with the language yet to do it without help. I don't want anyone to do it for me, but a healthy push in the right direction would be a great help. Here's what I have so far.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void printMenu();
#define SIZE 60
char largestCharacter(char str1[])
{ int max=0;
int i=0;
do{
if(max<str1[i])
max=str1[i];
}
while (str1[i]!='\0');
return max;
}
int howManyUpperCase(char str1[])
{
int i=0;
int count=0;
{
do{
if(str1[i]>=65 && str1[i]<=90)
count++;
i++;
}
while (str1[i] !='\0');
return count;
}
void doubleString(char str1[])
{
strcat(str1, str1);
}
void replaceChar(char str1[], char 1, char 2)
{
for(i=0; i<str1[];i++)
{
if(str[i]=='1')
{
'1'=='2';
}
}
}
//void insertChar(char str1[], char, char)
{
}
void printString (char str[])
{
printf("%s\n", str);
}
int main()
{
char inputStr[SIZE];
int i;
char char1, char2, char3;
int index;
char inputChar;
char choice;
char largest;
int count;
printf("Please enter a string and hit Return\n");
i = 0;
do
{
inputChar = getchar();
if (inputChar != '\n' && inputChar != '\r')
{
inputStr[i] = inputChar;
}
else
break;
i++;
} while (i < 30 && inputChar != '\n' && inputChar != '\r');
inputStr[i] = '\0';
printf("You entered the string: ");
printString(inputStr);
printf(" \n");
printMenu();
choice = 'Z';
do
{
printf("What action would you like to perform?\n");
choice = getchar();
getchar(); //to flush '\n'
choice = toupper(choice);
switch(choice)
{
case 'A':
largest = largestCharacter(inputStr);
printf("The largest Character is: %c\n", largest);
break;
case 'B':
count = howManyUpperCase(inputStr);
printf("The number of the upper case characters in the string is: %d\n", count);
break;
case 'C':
doubleString(inputStr);
printf("The string is doubled\n");
break;
case 'D':
printf("Please enter a character to be replaced: \n");
char1 = getchar();
getchar(); //to flush '\n'
printf("Please enter a character to replace with: \n");
char2 = getchar();
getchar(); //to flush '\n'
replaceChar(inputStr, char1, char2);
printf("The characters are replaced\n");
break;
case 'E':
printf("Please enter a character to be inserted: \n");
char3 = getchar();
getchar(); //to flush '\n'
printf("Please enter an index to insert: \n");
scanf("%d", &index);
getchar(); //to flush '\n'
insertChar(inputStr, char3, index);
printf("The character %c is inserted at %d\n", char3, index);
break;
case 'L':
printf("The string is: ");
printString(inputStr);
printf(" \n");
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
printf("Unknown action\n");
break;
}
} while (choice != 'Q');
return 0;
}
void printMenu()
{
printf("Choice\t\tAction\n");
printf("------\t\t------\n");
printf("A\t\tFind The Largest\n");
printf("B\t\tCount Upper Case Characters\n");
printf("C\t\tDouble String\n");
printf("D\t\tReplace Character\n");
printf("E\t\tInsert Character\n");
printf("L\t\tPrint String\n");
printf("Q\t\tQuit\n");
printf("?\t\tDisplay Help\n\n");
return;
}