1

I have a problem with my homework. I need to count quantity of upper case and quantity of vowels in string. Unfortunately, it always returns number 0 which looks as it doesn't change in function. Everything works until this one.

Here is my code:

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

char *StringChange(char *text, int *upper, int *chars);

int main(void) {
    char text[40];
    int upper, chars;

    puts("Type a string");
    gets(text);

    StringChange(text, &upper, &chars);

    puts("Change words to start with upper case and change white spece to *");
    puts(text);
    printf("Quantity of upper case in string: %d\n", upper);
    printf("Quantity of vowels: %d", chars);

    getch();
    return 0;
}

char *StringChange(char *text, int *upper, int *chars) {
    int i, length;

    length = strlen(text);

    for (i = 1; i <= length; i++) {
        if (text[i - 1] == '*' && (text[i] >= 'a' && text[i] <= 'z')) {
            text[i] = text[i] - 32;
        }
        if (text[i] == ' ') {
            text[i] = '*';
        }
        if (text[i] >= 'A' && text[i] <= 'Z') {
            *upper = *upper + 1;
            /* *upper++; that also doesn't work */
        }
        if (text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u' || text[i] == 'y') {
            *chars = *chars + 1;
            /* *chars++; that also doesn't work */
        }
    }

    if (text[0] >= 'a' && text[0] <= 'z') {
        text[0] = text[0] - 32;
    }

    return (text);
}
6
  • 2
    You should initialize upper and chars to zero somewhere... Commented May 19, 2018 at 14:25
  • What is this text[i-1] == '*' && (text[i] >= 'a' && text[i] <= 'z') for? Commented May 19, 2018 at 14:28
  • You might like to read this: How to debug small programs Commented May 19, 2018 at 14:30
  • That is a problem becouse if I turn all warnings and error, so no errors or warnings tells me. Otherwise I would not write anything there. Commented May 19, 2018 at 14:40
  • It seems that your problem is heavily input depending. Please add several pairs of sample input and resulting output. Commented May 19, 2018 at 14:42

2 Answers 2

2

I tried your code and I do get non-zero results -- depending on the input, of course, so maybe you are only testing on strings that produce zero.

However, the results are not always correct. There are two problems I found in the code:

1) As pointed out in a comment, you should initialize upper and chars to 0.

2) You are starting the loop at index 1, not index 0. I think you did this so you could look at text[i-1] inside the loop, but it is causing you to exclude the first character from your totals. You should start the loop index and 0 and figure out a different way to handle it within the loop. (Hint - note that the first if within the loop and the one following the loop have similar conditions and the same body.)

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

1 Comment

The problem was as you wrote. I'm starting the loop from 1 and forget about those int values to Null them. Thank you for your time.
1

There are multiple issues in your code:

  • you should never use gets().
  • the variables upper and chars are not initialized
  • the function StringChange make a special case of text[0] but does not update the counts for this initial byte.
  • you hard code the conversion of lowercase to uppercase for ASCII.
  • you should stop at the end of the string
  • all white space is not replaced, on whitespace followed by a lowercase letter.
  • uppercase vowels should be counted too.

Here is a modified version:

#include <stdio.h>

char *StringChange(char *text, int *upper, int *chars);

int main(void) {
    char text[200];
    int upper, vowels;

    puts("Type a string");
    if (fgets(text, sizeof text, stdin)) {
        StringChange(text, &upper, &chars);

        puts("Change words to start with upper case and change white space to *");
        puts(text);
        printf("Quantity of upper case in string: %d\n", upper);
        printf("Quantity of vowels: %d\n", vowels);
    }
    getchar();
    return 0;
}

char *StringChange(char *text, int *upper, int *vowels) {
    int i, at_start = 1;

    *upper = *vowels = 0;
    for (i = 0; text[i] != '\0'; i++) {
        char c = text[i];
        if (at_start && c >= 'a' && c <= 'z') {
            c += 'A' - 'a';
            text[i] = c;
        }
        if (c == ' ') {
            c = '*';
            text[i] = c;
            at_start = 1;
        } else {
            at_start = 0;
        }
        if (c >= 'A' && c <= 'Z') {
            (*upper)++;   // *upper++ would just increment the pointer, leading to undefined behavior
        }
        if (strchr("aeiouyAEIOUY", c) {
            (*vowels)++;
        }
    }
    return text;
}

2 Comments

It looks like you're much more advanced than me. I am a student and I wrote this code as best I can from my current skills. But it looks like I do not know how to write it right. Thank you for your post.
@OndřejJavorský: Everyone has been a beginner at some point... learning from examples is a good method. Study my proposed version and see how I fixed the various points enumerated.

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.