Tags down


counting words from user input

By : Sarthak Pattanayak
Date : October 18 2020, 06:10 AM
This might help you
the program cannot get out from the while loop while (c != EOF)
code :
while (c == ' ')
    c = getchar();
while (c == ' ' && c != EOF)
    c = getchar();
int main(void)
int ch;
while ((ch = getchar()) != EOF) {
    // process ch
c == ' '
#include <stddef.h>   // size_t
#include <stdbool.h>  // bool, true, false
#include <ctype.h>    // isalnum()
#include <stdio.h>    // getchar(), printf()

int main(void)
    size_t num_words = 0;
    bool in_word = false;
    int ch;

    while ((ch = getchar()) != EOF) {
        if (!in_word && isalnum(ch)) {  // new word begins. isalnum() or isalpha()
            in_word = true;             // ... depends on your definition of "word"
            continue;  // continue reading
        else if (in_word && isalnum(ch)) {  // still inside a word
            continue;  // continue reading

        in_word = false;  // when control gets here we're no longer inside a word
    }  // when control reaches the end of main() without encountering a return statement
       // the effect is the same as return 0; since C99 *)

    printf("Number of words: %zu\n\n", num_words);
for (int ch; (ch = getchar()) != EOF;)  // ...

Share : facebook icon twitter icon

Counting different words from a text input

By : user2514144
Date : March 29 2020, 07:55 AM
hope this fix your issue Following my comments, here are a few changes that may help you :
-Set palavra_atual to 0 and nova_palavra to true at the beginning of the while loop.
code :
#define MAX_PALAVRAS 1024
#define MAX_TAM_PALAVRA 32

typedef struct ocorrencia_ {
    char palavra[MAX_TAM_PALAVRA];
    int pos;
    int num_ocorrencias;

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

int main (int argc, char * argv[]){
    ocorrencia palavras[MAX_PALAVRAS];
    int i,palavras_diferentes=0,palavra_atual=0;
    char aux[MAX_TAM_PALAVRA];
    bool nova_palavra=true;
    for (i=0;i<MAX_PALAVRAS;i++){
    FILE * D = fopen("input.txt","r");
    while (!feof(D)){
        char aux2[MAX_TAM_PALAVRA];
            for (i=0;i<palavras_diferentes;i++){
                if (strcmp(palavras[i].palavra,aux)==0){
            if (nova_palavra==true){
                printf("new word %d %s\n",palavra_atual,aux);
    fclose (D);
    printf("diferent words=%i\n",palavras_diferentes);
    return (EXIT_SUCCESS);

JavaScript code for counting the words input into an input box

By : Maledikt
Date : March 29 2020, 07:55 AM
like below fixes the issue Why not splitting on empty character that textarea value and get arraylength:
Check the snippet below
code :
var myTextareaElement = document.getElementById("myWordsToCount");
myTextareaElement.onkeyup = function wordcount(wor) {

  var myText = this.value.trim();
  var wordsArray = myText.split(/\s+/g);
  var words = wordsArray.length;

  document.getElementById("wordcount").innerHTML = words;
<textarea id="myWordsToCount"></textarea>

<span id="wordcount"></span>

Counting number of vowels in input of given words

By : Bhupendra Bhupi
Date : March 29 2020, 07:55 AM
Does that help There are multiple problems that could explain a lower score:
You limit yourself to lines of 50 characters. The school test runs might have longer lines which you might mishandle. The specification says the words are limited to 50 characters, but the lines might be longer. You should use scanf() to read one word at a time. you exit the program whenever to see the sequence exit, even in the middle of a word, this is too strong and not required: the requirements as you stated is The program repeats this process until hitting the word exit (case-insensitive). In this case, terminate the program after printing the number of vowels in exit. You use toupper(c) where c has type char. This has potential undefined behavior if c is negative. You uppercase the character in isVowel(), but still compare it to the lower case vowels. c is a poor choice of name for a char *, better use s or str.
code :
#include <ctype.h>
#include <stdio.h>

int isVowel(char c);
int isExit(const char *c);

int main(void) {
    char s1[51];
    int i, v;

    while (scanf("%50s", s1) == 1) {
        for (i = v = 0; s1[i] != '\0'; i++) {
            if (isVowel(s1[i])) {
        printf("%d\n", v);
        if (isExit(s1))
    return 0;

int isVowel(char c) {
    c = toupper((unsigned char)c);
    return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

int isExit(const char *s) {
    return (toupper((unsigned char)s[0]) == 'E'
         && toupper((unsigned char)s[1]) == 'X'
         && toupper((unsigned char)s[2]) == 'I'
         && toupper((unsigned char)s[3]) == 'T'
         && s[4] == '\0');

Counting words from user input text file

By : New Chao
Date : March 29 2020, 07:55 AM
like below fixes the issue I am having trouble counting the amount of words in a given text file. Every time I input a text file name, the program returns "File not found". Here is the code I have so far: , You aren't splitting correctly. Split by " " instead of ""
code :
String []_words=cursor.split(" "); //-------------> Add Space

Counting words in a string input by the user

By : Jen
Date : March 29 2020, 07:55 AM
this will help Not a complete answer, because this is homework, but check the functions in . You basically want to find the next non-whitespace character in the string, then the next whitespace character, increment your counter, repeat until you reach the end of the string.
Related Posts Related Posts :
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • what will happen if we dont use free() for allocated memory
  • Why am I not getting the concatenated string?
  • not understandable pointers errors
  • Issue with function memory allocation
  • /usr/bin/ld: cannot find -lioutil in Makefile
  • Segmentation Fault when finding longest word in input
  • undefined reference to `palloc'
  • DMA transfer taking more time than CPU transfer
  • How can I make my code find solution while working at specific coordination?
  • How to fix implicit declaration of function?
  • Writing a file line by line in C
  • "accept" always returns 1, but no descriptor has been closed
  • How to fix 'Bad file descriptor' after implementing here-string redirection in my own shell
  • Why this output string to number?
  • Is there any way to make this adding an infinite?
  • for loop is iterating printf written after scanf only once
  • How to efficienty count from 0000 to 9999 in a digit display?
  • Implicit conversion and explicit conversion in c
  • Attempting to convert a value into 2s complement in C
  • else statement is resulting in identifier expected in c 13
  • Thread-safety vs atomicity in C
  • Segmentation fault - Trying to read binary file into memory
  • How to make a variable declared in constructor visible in the main file
  • What is the fastest way to reverse a power of two in C?
  • Reading and writing structure to binary file in C, then print structure elements
  • Why casting double to int might give different results?
  • Greedy algorithm in C not returning any values?
  • How to find base address of same char in string array using pointers in C language?
  • Quick question, why is scanf_s throwing an exception in runtime here? I am very confused
  • How can I compare two array of characters?
  • How should I avoid naming collisions with included libraries in C?
  • Code after while loop terminates does not run
  • How to capitalize first character of a word with all the characters in lower case previously and then print it
  • I need help to understand the syntax of pointers
  • How can I find the size of a const declared array in C?
  • Uninitialized C structure field
  • Hello World driver won't compile correctly
  • Is there a Gtk3 Widget that can display text as a terminal does?
  • How does the relational operator == work in C?
  • why value of parameter inside void main function in C is initialised with 1?
  • Dereferencing an Array of Pointers
  • Need help compiling .c file into shellcode
  • How can I store the 50ms before and after an audio event in a circular buffer?
  • This code is not calculating things right, any ideas why?
  • Difference between using a pointer to assign a value/just assigning it normally?
  • How to force compiler to promote variable to float when doing maths
  • Can these C codes be refactored this way?
  • c syntax error on common variables between different .c files
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk