0

I want to print argc to verify the words are being calculated correctly before moving to next section in my code. My code is:

int main(int argc, char *argv[])
{
    string s = get_string("Text:  ");
    //Read letters;
    n = strlen(s);

    printf("%d\n", n);
    printf("%d\n", argc);

Every time I run the program, argc = 1 always, even though the sentence typed has 4-5 words. I'm not sure why the program is not calculating argc correctly. Any help is greatly appreciated.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Pike1406
  • 5
  • 2

1 Answers1

0

So you asked to read text from input and calculate letter & word count:


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

int main()
{
    char a[1000] = {0};
    
    //read a line from input
    scanf("%[^\n]s", a);
    
    int word_count = 0;
    int letter_count = 0;
    int idx = 0; 
    
    // go through the line
    while (a[idx]){
        
        //skip spaces
        while(a[idx] && isspace(a[idx]))
            idx++;
            
        // if next char is a letter => we found a word
        if (a[idx])
            word_count++;
        
        //skip the word, increment number of letters
        while (a[idx] && !isspace(a[idx])){
            letter_count++;
            idx++;
        }
    }
    
    printf("word count = %d letter count = %d", word_count, letter_count);
   
    return 0;
}

enter image description here


EDIT : display line count also


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

int main()
{
    char a[1000] = {0};
    
    //read everyting from input until character '0' is found
    scanf("%[^0]s", a);
    
    int word_count = 0;
    int letter_count = 0;
    int sent_count = 0;
    int idx = 0; 
    
    // go through the lines
    while (a[idx]){
       
        //skip spaces
        //newline is also a space, check and increment counter if found
        while(a[idx] && isspace(a[idx])){
            if (a[idx] == '\n')
                sent_count++;
            idx++;
        }
            
        // if next char is a letter => we found a word
        if (a[idx])
            word_count++;
        
        //skip the word, increment number of letters
        while (a[idx] && !isspace(a[idx])){
            letter_count++;
            idx++;
        }
    }
    
    printf("word count = %d letter count = %d line count = %d", word_count, letter_count, sent_count);
   
    return 0;
}

enter image description here

Here's another way:

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

int main()
{
    char a[1000] = {0};
    
    int word_count = 0;
    int letter_count = 0;
    
    while (1){
        scanf("%s", a);
        
        // break when word starts with '0'
        if (a[0] == '0')
            break;
            
        word_count++;
        letter_count += strlen(a);
    }
    
    printf("word count = %d letter count = %d", word_count, letter_count);
    
    return 0;
}

This way reads input until word starting with character '0' is found

enter image description here

  • Thank you, argc (words = 5) is now calculating correcting but will have to figure out how to calculate word count correctly without entering the same sentence. ~/pset1/readability/ $ ./readability hi, what is your name Text: hi Letters: 2 Words: 5 – Pike1406 Jul 09 '20 at 17:47
  • Do you know if there is a way to read a string from console input and calculate argc that way instead of ./program ? Thanks! – Pike1406 Jul 09 '20 at 17:52
  • I think you want to do a for loop through argv and add each word length like so: for (int i = 1; i < argc; i++) sum += strlen(argv[i]); – Johnny Andrew Jul 09 '20 at 17:53
  • I don't think I fully understand your requirement. Read from console input and calculate the number of words? – Johnny Andrew Jul 09 '20 at 17:57
  • my program requirement is that the user must be prompted to enter text, then calculate number of letters and words based on the text input. Seems like doing a for loop is redundant since argc is already calculating the word count. I was hoping there would be a way to do that easily by passing the text string from the user prompt to argc vs. entering the sentence when starting program ./program but this does not meet the requirements of the assignment. Does this make sense? – Pike1406 Jul 09 '20 at 19:15
  • I am trying to understand each line of code on the first example and getting stuck on the first while loop. In debug mode, I can see that idx increments to 1, then immediately skips to the if statement. I'm having trouble understanding why the while evaluated to FALSE when idx = 1. Can you explain what idx is doing and how to properly read the while loop? Sorry for the newbie question here, I am just learning how to code.
    // go through the line
    while (a[idx]){
    while(a[idx] && isspace(a[idx]))
    idx++;
    – Pike1406 Jul 10 '20 at 15:57
  • the first example can be summarized by the following statement "skip spaces then skip words, until end" – Johnny Andrew Jul 10 '20 at 16:58
  • the 'skip spaces' while is just skipping the spaces :D, it is incrementing idx if there is a space at position idx – Johnny Andrew Jul 10 '20 at 17:00
  • also a[idx] is checking for ending '\0'. char arrays in c usually end in '\0' and they are called null terminated strings. – Johnny Andrew Jul 10 '20 at 17:02
  • so whenever a[idx] reached the character '\0' loop stops, and output is printed – Johnny Andrew Jul 10 '20 at 17:05
  • idk if I answered your question, perhaps you can reformulate – Johnny Andrew Jul 10 '20 at 17:09
  • OK, I think I understand. I tried to expand on the code to count sentences by adding a while loop directly after while (a[idx]){ while (a[idx] == '.' || a[idx] == '?' || a[idx] == '!' || a[idx] == '\n'); sent_count++; so that if any of those special characters are detected, i could count # of sentences using variable sent_count ++. But this boolean expression never evaluates to TRUE and increments. Thoughts? – Pike1406 Jul 10 '20 at 18:11
  • You can't count lines with my first example, my first comment was 'read a line from input' so it always reads only 1 line. – Johnny Andrew Jul 10 '20 at 18:21
  • I posted [link](https://stackoverflow.com/questions/62840203/sentence-count-doesnt-work-when-reading-text-string) before I saw your reply. since idx goes through each character, if I check for a special character first and increment if TRUE, I dont see why that would not work as a way to count sentences? – Pike1406 Jul 10 '20 at 18:27
  • I updated answer with a possible implementation of line count – Johnny Andrew Jul 10 '20 at 18:29
  • If the program goes through every character, why can we not check for special characters `.` `?` `!` and only increment when those characters are detected in a loop? Really appreciate all the help. – Pike1406 Jul 10 '20 at 18:48
  • you can do that outside the while loop, write another while loop that just does that. this way is more clear for you and you don't get confused by the other lines of code inside – Johnny Andrew Jul 10 '20 at 18:52