-1

this code is a word counting program but it does only for the letter "a" first it make an array of characters to print b[27] then make every character in b[27] this symbol '`' then plus all the plus all the characters which make a to z characters but when i run if (b[i] + j == c[f]) { it does not check all a to z it just check only 'a' character how do i fix this


main() {
  char b[27];
  char c[10];
  int counter = 0;

  for (int i = 0; i < 1; ++i) {
    b[i] = '`'; 

    for (int j = 1; j < 27; ++j) {
      b[i] + j;

      if (b[i] + j > 'z' || b[i] + j < '`') {
        break;
      } else {
        printf("%c\n", b[i] + j);

        for (int f = 0; f < 10; ++f) {

          while ((c[f] = getchar()) != EOF) {
            if (b[i] + j == c[f]) {
              ++counter;
              printf("%c = %i\n", b[i]+j, counter);
            }
          }
        }
      }
    }
  }
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 5
    A code with that many levels of nesting *must* be properly formatted. – Eugene Sh. Jun 13 '22 at 18:20
  • 1
    `for (int i = 0; i < 1; ++i)` What is this supposed to do? It will loop exactly 1 time. No need for a loop. – Gerhardh Jun 13 '22 at 18:21
  • 2
    `b[i] + j;` Does your compiler warn about expression without any effect? This will be evaluated but the result will not be used. Did you mean `+=` instead? – Gerhardh Jun 13 '22 at 18:22
  • @INDIAN KNIGHT It is unclear what this silly code is doing. Could you elaborate? – Vlad from Moscow Jun 13 '22 at 18:23
  • 1
    `((c[f] = getchar()) != EOF)`: `c` is an array of `char`. That may not be able to hold the value `EOF`. You must use `int` for return value of `getchar` and for comparing with `EOF`. – Gerhardh Jun 13 '22 at 18:23
  • let me try doing what you guys said – INDIAN KNIGHT Jun 13 '22 at 18:23
  • after doing b[i] += j the code now just evaulate only b i want to evaluate every character from a to z – INDIAN KNIGHT Jun 13 '22 at 18:25
  • *then make every character in b[27] this symbol '`'* ... That can be seen from your code. But *why* do you do that? What is the intented mechanism how that should work? – Gerhardh Jun 13 '22 at 18:26
  • i am doing this '`' to print a table of character a to z like this a = 1, b = 2 and so on but the ouput just print the length of entire string and prints only a = – INDIAN KNIGHT Jun 13 '22 at 18:35
  • sorry it is not fixed – INDIAN KNIGHT Jun 13 '22 at 18:36
  • how do i add value only to a specific character and print all character value – INDIAN KNIGHT Jun 13 '22 at 18:39
  • @INDIANKNIGHT 1) IMHO the main problem is that you wrote a program, before thinking how it should work. 2) You forgot to include libraries (which library has printf?) 3) You are using magic numbers. What is 27? What is 10? This is unreadable and also very dangerous. Please avoid this. 4) You said that this program is word counting program. Where is the input? Where are those words coming from? 5) I REALLY suggest that you first think of an algorithm. Write it down on a piece of paper (NOT as a C code) feed it with some words and checks if it works. After you verified it works, 'translate' to C – Melon Jun 14 '22 at 08:30
  • you are right i have actually program in only scripting language before like shell and python that's why i have a habit of directly going to program will first write algorithim thanks – INDIAN KNIGHT Jun 14 '22 at 08:38

1 Answers1

0

It seems as though you are trying to count how many times each alphabetical character appears in the input stream.

It appears you try to start with '`', because it comes one before 'a', as a way to build out an array that contains the alphabet. This is confusing.

This for loop seems to be an attempt to read at most ten characters,

for (int f = 0; f < 10; ++f) {
    while ((c[f] = getchar()) != EOF) {

but the while loop attempts to read the entire input stream.

Note that the negative value of EOF cannot be tested against properly if char (the type of c[f]) is unsigned on your system. Use int when handling the return value of getchar.

You have an unnecessary amount of nested loops. Use one loop to read the input. Use a separate loop to print the output.

With ASCII, 'a' - 'a' is 0, 'b' - 'a' is 1, and so on until 'z' - 'a' is 25. Use these values to index an array which keeps an individual count of each alphabetical character. Flip the operation to retrieve a character from an index (e.g., 'a' + 5 is 'f').

An example. Here we ignore non-alphabetic characters, and operate with case-insensitivity.

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

#define ALPHALEN 26

int main(void) {
    unsigned counts[ALPHALEN] = { 0 };
    int c;

    while (EOF != (c = getchar()))
        if (isalpha(c))
            ++counts[tolower(c) - 'a'];

    for (int i = 0; i < ALPHALEN; i++)
        printf("%c = %u\n", 'a' + i, counts[i]);
}

Alternatively, use fgets to read lines of input.

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

#define ALPHALEN 26

void alphacount(const char *str) {
    unsigned counts[ALPHALEN] = { 0 };

    while (*str) {
        unsigned char c = (unsigned char) *str++;

        if (isalpha(c))
            ++counts[tolower(c) - 'a'];
    }

    for (int i = 0; i < ALPHALEN; i++)
        printf("%c = %u\n", 'a' + i, counts[i]);
}

int main(void) {
    char buffer[512];

    while (1) {
        printf(">> ");

        if (!fgets(buffer, sizeof buffer, stdin))
            break;

        alphacount(buffer);
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • it does not print anything – INDIAN KNIGHT Jun 14 '22 at 06:01
  • @INDIANKNIGHT What is your input? How do you trigger `EOF` in your input? – Gerhardh Jun 14 '22 at 06:55
  • @Gerhardh in my progrram it just print the ouput when the user press enter and then again ask for input until it press control + c – INDIAN KNIGHT Jun 14 '22 at 08:24
  • @INDIANKNIGHT pressing Enter is not the same as `EOF`. Did you try with the code shown in the answer or with your own version? Adding lengthy code in comments is useless. Please add it at the bottom of your question. – Gerhardh Jun 14 '22 at 09:05
  • i am doing c programming language second edition chapter 1 exercise 14 and i have looked for many solution but they just don't show anything i want to use just in the chapter not othe things like fget – INDIAN KNIGHT Jun 14 '22 at 19:42
  • That exercise is described as *"Write a program to print a histogram of the frequencies of different characters in its input."*. The first program I've shown gets you half of the way there, counting character frequencies in the standard input stream. Just type whatever you want, and then [signal `EOF`](https://stackoverflow.com/q/1118957/2505965) to the program by pressing `CTRL-D` (`CTRL-Z` on Windows). I'm not sure I can make these examples, especially the first one, any simpler than they are. You may want to re-read the first chapter of the book if these concepts are still confusing. – Oka Jun 14 '22 at 20:06