Thread: counting a specific char

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    counting a specific char

    does anyone have an idea how can i do that?
    mainly, i think i can do it with a for loop, that will go over a buffer that will contain a line from the text file (*.txt) and each time will incrase the counting value by one.
    but how do i create a buffer and such a loop?

    thanks...

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    45

    reframe

    can u reframe your question clearly
    when u get there, there is isn't any there there.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    well, listen:
    i need to program a prog that its input will be a text file path, and its output will be the number that a specific char is there.
    for example:

    123.txt:
    ------------
    abc
    abc
    abc

    ------------

    and asumming that i'm looking for the char "a", the output would be: 3.

    can i do that?
    in the last post i wrote the outline of my idea to that...
    can you help me with that?

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    pretty easy, read the file one character a time, using fgetc(), and check each character, if it's 'a' (or any other character you choose), increment a counter, something like this:
    Code:
    FILE* fp = fopen("file.txt", "r");
    int counter = 0;
    char c;
    while ((c = fgetc(fp)) != EOF) {
        if (c == 'a') {
            counter++;
        }
    }
    fclose(fp);

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    45

    good code

    Hope u got a good code to do that in the prevoius post. Go ahead
    when u get there, there is isn't any there there.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    hehe that must of been my post
    Only the strong survives.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Thumbs up

    thanks guys, you really hekped me alot!

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    Hope you get a good mark in that class!

    ~arker

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Error counting occurrences
    By nick048 in forum C Programming
    Replies: 1
    Last Post: 04-09-2007, 02:08 PM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM