Home > database >  How do I join a user-entered string to three arrays and print the result?
How do I join a user-entered string to three arrays and print the result?

Time:06-20

I have a program for which I write a text and it calculates the number of letters in it, the number of words and the number of sentences, but I want to ask the user one question and then distribute the result or combine it into three arrays and then output the result once instead of asking the user each time to count the letters separately, the words separately, and the sentences on sharpness

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

int main(void) {
    //  character count
    string text = get_string("text: ");
    int number1 = 0;
    for (int i = 0; i < strlen(text); i  ) {
        if (text[i] != ' ' && isalpha(text[i])) {
            number1  ;
        }
    }

    //   Word counting calculator
    string words = get_string("text: ");
    int number2 = 0;
    for (int i = 0; i < strlen(words); i  ) {
        if (words[i] == ' ') {
            number2  ;
        }
    }

    //    Calculate the number of sentences
    string sentences = get_string("text: ");
    int number3 = 0;
    for (int i = 0; i < strlen(sentences); i  ) {
        if (sentences[i] == '?' || sentences[i] == '!' || sentences[i] == '.') {
            number3  ;
        }
    }
    printf("%i %i %i\n", number1, number2, number3);
}

CodePudding user response:

but I want to ask the user one question and then distribute the result or combine it into three arrays and then output the result once instead of asking the user each time to count the letters separately, the words separately, and the sentences separately

In that case just don't ask for input again:

...
    //  character count
    string text = get_string("text: ");
    int number1 = 0;
    for (int i = 0; i < strlen(text); i  ) {
        if (text[i] != ' ' && isalpha(text[i])) {
            number1  ;
        }
    }

    //   Word counting calculator
    int number2 = 0;
    for (int i = 0; i < strlen(text); i  ) {
        if (text[i] == ' ') {
            number2  ;
        }
    }

    //    Calculate the number of sentences
    int number3 = 0;
    for (int i = 0; i < strlen(text); i  ) {
        if (text[i] == '?' || text[i] == '!' || text[i] == '.') {
            number3  ;
        }
    }

And then you can even combine all the loops into one loop:

    int number1 = 0;
    int number2 = 0;
    int number3 = 0;

    string text = get_string("text: ");
    for (int i = 0; i < strlen(text); i  ) {

        //  character count
        if (text[i] != ' ' && isalpha(text[i])) {
            number1  ;
        }

        //   Word counting calculator
        if (text[i] == ' ') {
            number2  ;
        }

        //    Calculate the number of sentences
        if (text[i] == '?' || text[i] == '!' || text[i] == '.') {
            number3  ;
        }
    }

CodePudding user response:

I don't understand the relation between counting letters and combining strings. But anyway you can use built in functions in <string.h> for example you can combine two strings using: strcat(); .

  • Related