Help Counting Characters function

This is a discussion on Help Counting Characters function within the C Programming forums, part of the General Programming Boards category; Hello All; I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    3

    Help Counting Characters function

    Hello All;

    I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time. Not sure where I'm going wrong with this.

    Here is my full code:

    Code:
    /*
    Exercise 1-17 Write a program to print all input lines that
    are longer than 80 characters
    */
    
    
    #include<stdio.h>
    
    /* Declarations*/
    #define MAX_STRING_LEN 1000
    
    
    int count_characters(char S1[]);
    
    
    int main() {
    
    
        char S1[MAX_STRING_LEN];
        int length;
        int m = 80;
    
    
        /* Ask user for a string */
        printf("Input a string:\t");
        scanf("%s", S1);
    
    
        /* obtain the length of the string*/
        length = count_characters(S1);
    
    
        /* print if the string length is over the value of "m" otherwise say why not */
    
    
        if (length > m)
            printf("String length is %d\nOriginal String input is: %s\n", length, S1);
        else
            printf("String length is %d\nWill not print because is equal or less than %d\n", length, m);
    }
    
    
    /* counts the characters in a string */
    int count_characters(char s[])
    {
        int nc =0;
        int c;
        for (nc = 0; (c = getchar()) != '\n'; ++nc);
        return nc;
    }
    so I was trying to debug my count_characters() function and this is the code if I was to run it seperately:

    Code:
    #include <stdio.h>
    
    
    /* counts character of a string*/
    
    
    main()
    {
    
    
        int nc = 0;
        int c;
        for (nc = 0; (c = getchar()) != '\n'; ++nc);
        printf("Number of characters = %d\n", nc);
    }
    which works, so I'm a little confused, could anyone give me a hint on what I'm doing wrong?

    Thanks

    note: I'm trying to learn C and so far I'm in chapter 1&2 of the "C programming Language" by Dennis M. Ritchie

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your count_characters function needs to read from s, not from standard in (ie using getchar).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help counting characters
    By dom89 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 10:25 PM
  2. Counting Characters
    By jrdoran in forum C Programming
    Replies: 2
    Last Post: 11-28-2006, 08:06 PM
  3. Counting characters
    By jmajeremy in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 10:14 AM
  4. Counting Characters HELP !!
    By Zozo17 in forum C Programming
    Replies: 2
    Last Post: 03-01-2005, 08:00 PM
  5. Counting characters
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-29-2003, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21