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

  • SEARCH
  • Home
  • 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 6654583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:24:25+00:00 2026-05-26T01:24:25+00:00

This is a program to find the number of characters in a string. But

  • 0

This is a program to find the number of characters in a string. But it counts the wrong number of characters. Is it counting the white space too ? Even if that is true how can the total number be 89 ? (see output below)

#include <stdio.h>
#include <conio.h>


void occurrence(char str[100], char ch)
{
 int count=0,max =0,i;
 for(i=0;i<=100;i++)
 {
   if(str[i]!='\0')
   {
     max = max + 1;

     if(str[i]==ch)
     {
      count = count + 1;
     }
   }
 }

 printf("\nTotal Number of characters : %d\n",max);
 printf("\nNumber of Occurrences of %c : %d\n",ch,count);
}

int main(void)
{
 void occurrence(char [], char);
 int chk;
 char str[100], ch, buffer;

 clrscr();
 printf("Enter a string : \n");
 gets(str);

 printf("Do you want to find the number of occurences \nof a particular character (Y = 1 / N = 0) ?  ");
 scanf("%d", &chk);

 do
 {
  if (chk==1)
  {
   buffer = getchar(); //dummy varaiable to catch input \n
   printf("Enter a Character : ");
   ch = getchar();
   occurrence(str,ch);
   printf("\n\nDo you want to check the number of occurences \nof another character (Y = 1 / N = 0) ? ");
   scanf("%d", &chk);
  }
  else
  {
   exit();
  }
 }
 while(chk);

 return 0;
}

enter image description here

  • 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-05-26T01:24:26+00:00Added an answer on May 26, 2026 at 1:24 am

    There are two important things wrong with the for loop that counts characters:

    1. It goes from 0 to 100, when it should go from 0 to 99. If you allocate a 100-element array, then the index of the highest element is 99, for a total of a hundred elements. Traditionally the exit condition for the loop would be i < 100, not i <= 100.

    2. It keeps going after the ‘\0’ is found. The ‘\0’ character marks the end of the string, and you should not count any characters after it. Some of the characters after the ‘\0’ may themselves be ‘\0’s, and so you won’t count them; but there could be any other kind of garbage there as well, and those will mess up your count. You must figure out how to change your for loop to exit as soon as the ‘\0’ character is found, and not count anything else after that point.

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

Sidebar

Related Questions

This program stores pairs in a map, counting the number of times a word
I have to write a program to find a rational number that has a
I am writing this java program to find all the prime numbers up to
Can someone help me with this: This is a program to find all the
I am developing a program where I find myself doing this like this a
I'm currently doing this to replace extended-ascii characters with their HTML-entity-number equivalents: s.encode('ascii', 'xmlcharrefreplace')
I made a program to find if a number belongs to fibonacci series or
I've been able to find similar, but not identical questions to this one. How
I wrote this small program to find all occurrences of a substring in a
I need to write a C program to find the Perfect Number.. main() {

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.