Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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++;
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading