Hello there, and thank you in advance for helping me with this! I am working on a program for my class, which is supposed to perform multiple functions on a string that the user enters. I have posted the code that was given to us, as well as one of my functions, which counts the instances of a particular character (MY code is the countCharacter function at the very top, and what was given by my teacher is everything else, the cases and all that).
I am having a problem with the first function, the countCharacter one. I am getting multiple errors:
line 10: syntax error before "list"
line 13: `list' undeclared (first use in this function)
line 14: `desired' undeclared (first use in this function)
Can someone help me figure out what I am doing wrong? I didn't write any programs over the summer and have somewhat lost the basics, so it may be something really simple that I am just not thinking about.
Thanks for any help you can provide!!
#include <stdio.h>
#include <ctype.h>
void printMenu();
//constant is defined
const int SIZE = 50;
int countCharacter(char desired, char[] list)
{
int i=0, count=0 ;
for(i = 0; list[i] != 0; i++)
if(list[i] == desired)
count++;
return count;
}
int main()
{
char inputStr[SIZE];
int i;
char inputChar;
char choice;
char compare;
int count;
printf("Please enter a string and hit Return\n");
i = 0;
//read character by character
do
{
inputChar = getchar(); //read one character
if (inputChar != '\n' && inputChar != '\r')
{
inputStr[i] = inputChar;
}
i++;
} while (i < SIZE && inputChar != '\n' && inputChar != '\r');
inputStr[i] = '\0'; //mark the end of the string
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':
printf("Please enter a character to count: \n");
compare = getchar();
getchar(); //to flush '\n'
count = countCharacter(compare, inputStr);
printf("The number of the character in the string is: %d\n", count);
break;
case 'B':
convertToLowercase(inputStr);
printf("The string is converted to lower case\n");
break;
case 'C':
convertToUppercase(inputStr);
printf("The string is converted to upper case\n");
break;
case 'D':
reverseString(inputStr);
printf("The string is reversed\n");
break;
case 'E':
sortCharacters(inputStr);
printf("The characters in the string is sorted\n");
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\tCount Character\n");
printf("B\t\tConvert To Lowercase\n");
printf("C\t\tConvert To Uppercase\n");
printf("D\t\tReverse String\n");
printf("E\t\tSort Characters\n");
printf("L\t\tPrint String\n");
printf("Q\t\tQuit\n");
printf("?\t\tDisplay Help\n\n");
return;
}