Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:30:19+00:00 2026-06-12T02:30:19+00:00

/* stringlength * input: str, pointer to a string * output: integer representing the

  • 0
/* stringlength
 * input: str, pointer to a string
 * output: integer representing the length of string str, 
 * not counting the terminating character.
 *
 * You may NOT call ANY functions within this function.
 */
int stringlength(char *str)
{
    // count the number of characters in str
  int count=0,k;
  for (k=0; str[k] != '\0';k++)
    count++;
    return count;
}

/* countchars
 * inputs: character c, string str
 * output:  The number of instances of c in the string str
 * You may not call ANY function calls within this function.
 */
int countchars(char c, char *str)
{
    // count the number of times c is found in str
  int k,count=0;
  for (k=0;str[k]=='\0';k++)
    {
      if (str[k] == c)
    count++;
      else;
    }

    return count;
}

/* countlines
 * input: char *filename - string containing the filename
 * output: integer representing the number of lines in the file
 */
int countlines(char *filename)
{
    // count the number of lines in the file called filename
  FILE *f = fopen(filename,"r");
    char ch;
    int lines=0;
  f = fopen(filename,"r");

  do{
    ch = fgetc(f);
    if( ch == '\n')
      lines++;
  }while( ch != EOF );

return lines;
}

I need help with these three different functions that I am implementing in my program. I am a beginner so go easy on me, the countlines function is giving me the most trouble. If anyone could explain why not or why these functions will work, it would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:30:21+00:00Added an answer on June 12, 2026 at 2:30 am

    There are a number of problems in countlines():

    1. You open the file twice, but overwrite the first FILE * value with the second, so there’s no way you can close it. This is a minor problem.

    2. The major problem is that the function fgetc() returns an int, not a char. In particular, EOF is a value different from every char.

    3. The code does not close the file before returning. Generally, if you open a file in a function, then you should close it. If you don’t, you have to pass the file pointer back to the calling code so that it can close it.

    4. The do ... while loop is seldom correct for an input loop (a while loop testing at the top is almost always much cleaner and clearer) but at least you weren’t using feof().

      int countlines(char *filename)
      {
          FILE *fp = fopen(filename,"r");
          int ch;
          int lines = 0;
          if (fp == 0)
              return lines;
      
          while ((ch = fgetc(fp)) != EOF)
          {
              if (ch == '\n')
                  lines++;
          }
      
          fclose(fp);
      
          return lines;
      }
      

    When you use char instead, one of two things happens:

    • If your char type is signed, then a real character (often ÿ — y-umlaut, U+00FF, LATIN SMALL LETTER Y WITH DIAERESIS) also matches EOF so you can stop reading before you reach end of file.
    • If your char type is unsigned, no value will ever match EOF so the loop will never stop.

    In stringlength(), you have two variables count and k that are carefully kept at the same value; you only need one of the two.


    Apart from raggedy indentation (endemic in the code shown — and definitely something to be avoided), and the unnecessary and pointless else; which does absolutely nothing, the code for countchars() looks OK (late addition) … has the condition in the for loop inverted; it should be str[k] != '\0', of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Giving a Input String, the string length will not more than 30, the output
Using fgets to input a string, I have doubts related to length of string
So I'm creating a random string value: var randomString = function(stringLength) { var i
string = Yellow puts string[1..string.length] -> outputs ellow Is there a shorter way to
How to get the string length in bytes in nodejs? If I have a
I want to get the string length when a key is pressed like StackOverflow
does fortran have a maximum 'string' length? i am going to be reading lines
strace wget grooveshark.com stops with strange socket calls with wrong string length and I
is it possible in SQLAlchemy to enforce maximum string length of value assigned to
I want to sort a list of strings based on the string length. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.