Thread: Coding why wrong/logic cant understand

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Question Coding why wrong/logic cant understand

    Am tyring to solve simple string related problem.Its almost solve but i faced some problem and can't understand.so i need your help for get my soluton .
    here is my coding

    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
       // freopen("ic.txt","r",stdin);
        char a[200];
        int i,j=0,l,c=0,k=0;
        while(fgets(a,sizeof(a),stdin)!=EOF)
        {
        l=strlen(a);
        for(i=0;i<l-1;i++)
        {
    //printf("%d ",i);
    //j=i;
    if((a[0]==32)||(a[0]==',')||(a[0]=='.'))
    {
        k++;
    
    
               while((a[k]==32)||(a[k]==',')||(a[k]=='.'))
               {
    
                   //i++;
                   k++;
    
    
               }
               i=k;
    }
    
            if((a[i+1]==32)||(a[i+1]==',')||(a[i+1]=='.'))
            {
               c++;
               //j++;
    
               while((a[i+1]==32)||(a[i+1]==',')||(a[i+1]=='.'))
               {
    
                   i++;
                   //j++;
    
    
               }
            }
    
        }
    
    
    printf("\n%d %d %d\n",(c+1),k,i);
    c=k=0;
    //puts(a);
    }
    }
    problem 1

    here i got warning for using EOF. i dont know why i waring.
    /media/sda9/c partice/string/word-count.c||In function ‘main’:|
    /media/sda9/c partice/string/word-count.c|7|warning: comparison between pointer and integer [enabled by default]|
    ||=== Build finished: 0 errors, 1 warnings ===|
    how i solve this warning?

    problem number 2

    if i took my input these[space or . or ,]
    then i got k value (i-1)
    i dont know why.

    here is simple input
    Code:
    ...raihan...,,  riya,,,...hmmm
    simple output
    Code:
    3 29 30
    here i got k=(i-1)
    but how?

    pls everybody debug my code and tell me any more error.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Code:
    while(fgets(a,sizeof(a),stdin)!=NULL)
    try this

  3. #3
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    1. fgets() return string on success, and NULL on error or when end of file occurs while no characters have been read.

    You cannot compare the return of fgets() with EOF. So apply the change @theju112 suggested. This should get you away with warning.

    BTW, can you tell me what are you trying to do with this code? May be then I can help with the rest of your questions.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    am trying to count word . fgets take input if its find space or . or , then word count++ if it find multiple space or . or , then increasse value i till find letter.
    but i couldn't get my 2nd ques ans
    problem number 2

    if i took my input these[space or . or ,]
    then i got k value (i-1)
    i dont know why.

    here is simple input
    Code:
    ...raihan...,,  riya,,,...hmmm
    simple output
    Code:
    3 29 30
    here i got k=(i-1)
    but how?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by raihan004 View Post
    but i couldn't get my 2nd ques ans
    That's probably because nobody here understands it. Perhaps you try to rephrase it and tell us what you expect as the correct output (i.e. what should the three numbers 3, 29, 30 represent?)
    Your use of meaningless single letter variable names doesn't help either.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    pls everybody debug my code and tell me any more error.
    Why don't you debug your own code? It's your job!

  7. #7
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Sorry Buddy..I am still not able to understand what are you upto. You better rephrase it.
    Quote Originally Posted by raihan004 View Post
    am trying to count word . fgets take input if its find space or . or , then word count++ if it find multiple space or . or , then increasse value i till find letter.
    but i couldn't get my 2nd ques ans

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is there anything wrong in sudoku logic ?
    By suryak in forum C Programming
    Replies: 5
    Last Post: 08-09-2011, 01:19 AM
  2. Recursive function, trying to understand the logic.
    By csharp100 in forum C Programming
    Replies: 7
    Last Post: 11-16-2010, 11:38 AM
  3. what is wrong with the logic in my loop?
    By tranman1 in forum C Programming
    Replies: 4
    Last Post: 03-10-2010, 08:48 PM
  4. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  5. Logic all wrong
    By nizbit in forum C Programming
    Replies: 4
    Last Post: 10-17-2004, 05:15 PM