#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STRING_LENGTH 1024
int main (void) {
//Scans in a string from the user.
char buffer[MAX_STRING_LENGTH];
fgets(buffer, MAX_STRING_LENGTH, stdin);
int string_length = strlen(string) - 1;
for (int i = 0; i < string_length; i++) {
if (string[i] == ' ') {
i -= 1;
} else if (string[i] > 'A' && string[i] < 'Z') {
if (i % 2 == 0) {
string[i] += 32;
}
} else if (string[i] > 'a' && string[i] < 'z') {
if (i % 2 == 1) {
string[i] -= 32;
}
}
}
// Prints resulting string.
printf("%s", buffer);
return 0;
}
I've tried to skip the whitespaces using:
if (string[i] == ' ') {
i -= 1;
}
but it doesn't work
could someone plz explain why this didn't work and what I can do instead to have the code skip whitespaces?