Reputation: 1911
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){
}
Upvotes: 1
Views: 41
Reputation: 780871
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.
Upvotes: 1