Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include <stdio.h>
#include <stdlib.h>

void fun(int choice);
int array[10];

void main()
{
   int choice,i;
   scanf("%d",&choice);
   for(i=0;i<choice;i++)
   {
       fun(i);

   }

for(i=0;i<choice;i++)
    printf("\n%d\n",array[i]);
}

void fun(int choice)
{
      char string[50];
      int k,count1=0,count2=0,j=0,temp=0;
     fgets(string,50,stdin);
     j=strlen(string);


    for(k=0;k<j;k++)
    {
        if(string[k]=='a')
        count1++;
    else if(string[k]=='b')
        count2++;
    else

    break;
    }

     temp=count1-count2;
     if(temp<0)
        array[choice]=count1;
     else if(temp>0)
        array[choice]=count2;
     else
       array[choice]=0;

}


What I have tried:

i need to enter the test case choice=3
the loop seems to go 3 times

while the fgets in function fun is taking only 2 inputs .it take a null character as a first input .why ?how to correct it
Posted
Updated 7-Jul-16 4:29am

Maybe take a look at this[^] thread on the subject.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900