1

I have a relatively simple problem. A user is supposed to run the program,type in a message of 30 characters or less and then the case of the individual letters of the messages will be toggled.(lowercase letters will be made upercase and vice versa). Everything is fine except for the part of changing the case. I am supposed to use a pointer to a char in an array and pass it as a function argument to toggle_case. Please assist me on how to achieve this. Here is the code.

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

void read_message(char *message, int *message_size);
void toggle_case(char *character);
void count_cv(char* message);

int main() {

   char message[30];
   int message_size;

   char *message_pointer;
   message_pointer = message;

   read_message(message, &message_size);
   int i = 0;
   for(i =0; i<30; i++){
        toggle_case(&message_pointer[i]);
    }
   printf("New string: %s",message);
   return 0;
}

void read_message(char *message, int *message_size){
    printf("Enter your string (maximum 30 characters): ");
    fgets(message, 30, stdin);
}

void toggle_case(char *character){
    //check if character is a letter
    if (isalpha(*character)) {
    //Check if the character is upper case
    if(isupper(*character)){
        //convert the character to lower case
        tolower(*character);
    } else { 
        //Check if the character is lower case
        //convert the character to upper case
        toupper(*character);
    }
}
}

void count_cv(char* message){

}
2
  • What exactly is your difficulty? Does the code not compile, does it crash, does it produce the wrong output, etc? Explain it clearly to us so we can know what to help you with. It just look like you are not saving the result back into the original character in toggle_case. If that is your problem, then either assign the result within toggle_case or return a result for the caller to assign. Commented Apr 4, 2016 at 23:26
  • message_size doesn't appear to be used at all... Commented Apr 4, 2016 at 23:28

1 Answer 1

1

tolower and toupper return the new character, you need to assign it back to the location you're updating.

if (isupper(*character)) {
    *character = tolower(*character);
} else {
    *character = toupper(*character);
}

BTW, you don't need to check if the character is a letter first. tolower and toupper will simply return the character unchanged if it's not a letter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.