#How can I skip whitespaces when iterating through a string

15 messages · Page 1 of 1 (latest)

teal moon
#
#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?

forest wagonBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

teal moon
#

im using string instead of buffer cause that part was in a function and i forgot to replace

hollow kettle
#

what you would want is

if (string[i] == ' ') {
  continue;
}
teal moon
#

just think of string as buffer or vice versa

hollow kettle
#

continue will end the current iteration of the loop and move to the next step

teal moon
#

tried continue just now, didn't work

#
void make_alternating(char string[MAX_STRING_LENGTH]);

int main (void) {

    //Scans in a string from the user.
    char buffer[MAX_STRING_LENGTH];
    fgets(buffer, MAX_STRING_LENGTH, stdin);

    // Runs function
    make_alternating(buffer);

    // Prints resulting string.
    printf("%s", buffer);

    return 0;
}

void make_alternating(char string[MAX_STRING_LENGTH]) {

    int string_length = strlen(string) - 1;

    for (int i = 0; i < string_length; i++) {
        if (string[i] == ' ') {
            continue;
        } 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;
            }
        }

    }
}

This is full code

hollow kettle
#

given an input, what the expected output, and your actual output

teal moon
#

Input:
The quick brown fox jumps over the lazy dog
Expected Output:
tHe QuIcK bRoWn FoX jUmPs OvEr ThE lAzY dOg
My Output:
tHe qUiCk bRoWn fOx jUmPs oVeR ThE Lazy dOg

#

my code is reading the whitespaces

hollow kettle
#

Oh in that sense
What you want is to keep track of a separate variable which keeps track of how many non whitespaces you come accross

worthy coral
#

The problem with your code is that even if you continue, i will still change for the next iteration, hence implicitly counting the whitespace

forest wagonBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.