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;
}

>>int countCharacter(char desired, char[] list)

The [] is in the wrong place -- should be char list[]

Thank you! That fixed my errors. Now, the next method is to reverse a string. How do I do this, since the input is supposed to be formatted as an array of letters? What I mean is this: my teacher gave us the headers for all the functions, and the header for this one is the following:

void reverseString (char[])

So how do I proceed, given that the input is not a string but is instead an array? I think I need to find the size of the array, but I forget how to do that. And then, I know that I need to use a for loop, and run that through all of the values in the array, but I am not sure what else I need to do.

Thank you so much for all of your help!!

In C language, a string and a character array are normally the same thing, unless specifically told that the character array contains binary data.

>>I think I need to find the size of the array, but I forget how to do that.
The best you can do is find the string length. The function strlen() will return that for you.

Okay...I really need to get the differences between the two straight in my mind. Thank you! Here is what I have for this function so far, but I am getting a few errors:

line 22 (here, line 3): variable-sized object may not be initialized
line 26 (here, line 7): request for member `charAt' in something not a structure or union

void reverseString (char list[])
{
     char reverse[strlen(list)] = "";
     int i=0;
     int N = strlen(list);
     for (i = 0; i < N; i++)
     reverse = list.charAt(i) + reverse;
     // return reverse;
}

Is my code close to being right, except for these two errors? Also, can you tell me how to fix these errors? And last, how do I return the string, since the function header type is void?

Thank you SO MUCH for your help!!

Well, I fixed one of the errors, but I am still getting the one at line 26 (or line 7 in the code below). My code now looks like this:

void reverseString (char list[])
{
     char reverse[strlen(list)];
     int i=0;
     int N = strlen(list);
     for (i = 0; i < N; i++)
         reverse = list.charAt(i) + reverse;
     printf("%s\n", reverse);
}

And like I said, I am still getting the error that says: "request for member `charAt' in something not a structure or union". How do I fix this? Thank you for all of your help!

list is NOT a class or structure, therefore it has no methods, such as CharAt(). You may be confusing a character array with c++'s std::string class. Just use [] to index into a character array reverse = list[i] + reverse; But that doesn't work either for a similar reason -- you can not append to a character array like that. If you want to add something to the beginning of a character array you have to move everything right one character to open up the room for the new character. Note that strcpy() will not work here because sourcee and destination are overlapping addresses.

for (i = 0; i < N; i++)
   {
         // move everything right 1 position
         memmov( reverse+1, reverse, strlen(reverse)+1)
         reverse[0] = list[i];
    }

Okay...that makes sense. So my function now looks like this:

void reverseString (char list[])
{
     char reverse[strlen(list)];
     int i=0;
     int N = strlen(list);
     for (i = 0; i < N; i++)
     {
         memmov( reverse+1, reverse, strlen(reverse)+1);
         reverse[0] = list[i];
     }
     printf("%s\n", reverse);
}

But I am still getting an error. It says: "[Linker error] undefined reference to `memmov'". What does this mean? Do I need to define memmov so that I can use it?

Also, can you help me figure out how to do the reversal? Because I don't think my code right now takes care of that.

Okay, I realized that it was probably "memmove," and then I also figured out how to do the reversal! So now I have five of the six functions working! Thank you for helping me get my program to this point!! Can you help me with the last function?

The last function is supposed to sort the characters in the string, so that all the A's appear first, then the B's, then C's, etc. I don't really know how to start this function...could you give me some advice about how to begin?

Thank you so much for all your help, Ancient Dragon!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.