-1

the while loop is not working here.there is no compilation error and the print statement is also getting executed in the beggining.the code works fine without the while loop.the code is to print the most repeating letter in a string and the number of times it is repeated

#include <stdio.h>
#include <string.h>

int main(void)
{
    int t;
    int tempC;
    scanf("%d",&t);
    while ( (tempC = getchar()) != '\n' && tempC != EOF );
    while(t--)
   {
      char c[100];
      char r='z';
      int i,j=0,count,a,k,amx;
      gets(c);
      k=strlen(c);
      for(i=0,amx=1;i<k;i++)
      {
         if(c[i]!=0)
         {
            for(j=0,count=1;j<k;j++)
            {
               if(c[i]==c[j])
               {
                  if(i!=j)
                  {
                     count++;
                     c[j]=0;
                  }
                  a=count;
                  if(a>amx||(a==amx&&(c[i]<r)))
                  {
                     amx=a;
                     r=c[i];
                  } 
               }
            }
         }

      }
      printf("%d %c\n",amx,r);
   }

}
v.sharath chandra
  • 81
  • 1
  • 2
  • 11
  • Don't use `gets()` use `fgets()`. – Iharob Al Asimi Jul 01 '15 at 17:29
  • Please elaborate *the while loop is not working*. What do you expect to see? What are you seeing that is different from what you expect to see. – R Sahu Jul 01 '15 at 17:37
  • never used it before.can you tell me how to implement it.i am begginer – v.sharath chandra Jul 01 '15 at 17:41
  • i was expecting abbbbb gives 5 b bbaa gives 2 a but when i use while loop then i enter the no: of test cases like ex: 4 it prints 1 4 and ends – v.sharath chandra Jul 01 '15 at 17:42
  • Your check for i!=j may make the count one less than you expect. – Robert Jacobs Jul 01 '15 at 18:32
  • @RobertJacobs that's why i intiliased count=1 not 0 – v.sharath chandra Jul 01 '15 at 18:36
  • also, `if (c[i] != 0)` is redundant, as you are going from `i = 0` while `i < k` which you have initialized to be `strlen(c)` (to get the length it searches the string for a `c[i] == 0`, so you get in `k` the actual number of charactes, all different from `\0`) So the `if` statement is always true. – Luis Colorado Jul 02 '15 at 07:41
  • By the way, you had better done instead of `k = strlen(c); for(i = 0; i < k; i++)` the following: `for (i = 0; c[i] != 0; i++)` because doing so you save one pass through the string (the one made by strlen just to search the end of the string). – Luis Colorado Jul 02 '15 at 07:44
  • @LuisColorado the inner for loop makes for(j=0;j – v.sharath chandra Jul 02 '15 at 08:27
  • @nani that's right, but supposing you go upstairs and you don't pass beyond `i` it's difficult to find values `== 0` beyond that place. Either case, the implementation is difficult to follow. – Luis Colorado Jul 02 '15 at 08:35

3 Answers3

0

After the following line is executed,

scanf("%d",&t);

the newline character is still left in the input stream. The next call to gets ends up reading just the newline.

You need to add code to ignore the rest of the line after readning t.

scanf("%d",&t);

// Ignore the rest of the line from the input stream.
char c;
while ( (c = getchar()) != '\n' && c != EOF);

Also, don't use gets.

Further reading: Why is the gets function so dangerous that it should not be used?.

Use fgets instead.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

According to what you mentioned, while loop works fine and just use scanf() in place of gets()(Don't use gets()) and then printf() will also be executed after loop.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
0

You can also ask directly to your gcc compiler:

gcc test.c -o test.x -std=c99 -Wall -Wextra

And to be sure, try other options: -std=gnu99, -std=gnu89, -std=c89, and my preferred:

gcc test.c -o test.x -Wall -Wextra -ansi -pedantic-errors -O0
DrBeco
  • 11,237
  • 9
  • 59
  • 76
  • I ran your preferred code it showed ISO c90 forbids mixed declarations and code int tempc; cand.c :43:1:warning control reaches end of a non void function – v.sharath chandra Jul 02 '15 at 11:09
  • Can you give me any reference so that I can figure it out? – v.sharath chandra Jul 02 '15 at 11:11
  • You cannot mix declarations (like `int i;`) with commands (anything, like a `printf()` or an `if()`). You need to make all declarations in the top. And the control reaching `non-void`, is because only `void` functions are allowed to finish without `return`. A `int f()`, for example, must use a `return X;`. Remember: gcc is your second best friend, after google. – DrBeco Jul 04 '15 at 03:03
  • As for references, copy and past the error code inside google. It will give you some "north". Example: this google (https://www.google.com.br/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=reaches%20end%20of%20non-void%20function) gives you this stackoverflow answer: (http://stackoverflow.com/questions/2440337/warning-control-reaches-end-of-non-void-function-iphone) – DrBeco Jul 04 '15 at 03:06