plez where is the bug?? here is the code

This is a discussion on plez where is the bug?? here is the code within the C Programming forums, part of the General Programming Boards category; help me to find the bug .. the code must print the same word that appears in the ..First sentences ...

  1. #1
    Registered User jaxlle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    Red face plez where is the bug?? here is the code

    help me to find the bug ..
    the code must print the same word that appears in the ..First sentences & Second sentences.. and must have the Max Length.
    char First[]="Ann,Bob and Bill Abraham live in Alabama."
    char Second[]="Alabama is where alabama Anne Abrahamson lives."
    the output= Alabama
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    int WordLength(char *);
    int WordCompare(char *,char *);
    char *GoNextWord(char *);
    char *max_equivalent_words(char *pFirst , char *pSecond){
        char *pTemp1=pFirst,*pTemp2=pSecond,*MaxP;int Len=0,MaxLen=0;
    	while(*pTemp1 && isalpha(*pTemp1)==0)
    		while(*pTemp2 && isalpha(*pTemp2)==0){
    			if(WordCompare(pTemp1,pTemp2)){
    				Len=WordLength(pTemp1);
    				if(Len>MaxLen){
    					MaxP=pTemp1;
    					MaxLen=Len;
    				}
    				pTemp2=GoNextWord(pTemp2);
    			}
    		pTemp2=pSecond;
    		pTemp1=GoNextWord(pTemp1);
    	}
    	return(MaxP);
    
    }
    char *GoNextWord(char *pFirst){
       char *p1;
       p1=pFirst;
       while(isalpha(*p1) && *p1!='\0')
           p1++;
       return (++p1);
    }
    
    void PrintWord(char *pWord){
       char *p1=pWord;
       while(*p1 && isalpha(*p1)){
    	   printf("%c",*p1);
    	   p1++;
       } 
    }
    
    int WordCompare(char *pFirst,char *pSecond){
       char *p1=pFirst,*p2=pSecond;int sum=0;
       if(WordLength(p1)!=WordLength(p2))
    	   return 0;
       while(isalpha(*p1)){
    	   if(*p1==*p2);
    		   sum=sum;
    	   p1++; p2++;
    	   }
       if(sum==WordLength(p1))
    		   return 1;
    }
    
    int WordLength(char *Word){
       char *p1;int i=0;
       p1=Word;
       while(isalpha(*p1)){
       p1++;
       i=i+1;
       }
       return(i);
    }
    
    int main() {
       char *p1,*p2;
       char First[]="Ann,Bob and Bill Abraham live in Alabama.";
       char Second[]="Alabama is where alabama Anne Abrahamson lives.";
       p1=max_equivalent_words(First,Second);
       PrintWord(p1);
       return 0;
    }

  2. #2
    Registered User jaxlle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    it's me agin

    i want to re wright the
    Code:
    sum=sum+1;
    Line 36 in WordCompare Function

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    why dont u do this for the problem tokenize the two string ref this artical tokensize the string

    you will get all the words seperated. Then comparing with words will become easy for you rather than comparing two literal string searching for a simplier word.

    The algorithm is

    1. Take string 1 token it and store it
    2. Take string 2 tokenize it ans store it
    3. Now compare Str1Tokenize[i] = str1tokenize2[i] on print it.
    else
    i++

    hope this will make your life bit easier

    ssharish2005

  4. #4
    Registered User jaxlle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    Wink ....

    thank you for you advice.... i learned new things from your advice ...
    but i am studying CS and i must use these algorithm cuz my homework is about pointers
    and they want me to Suffer while doing the question....hard in the battle..
    hard in the learning easy in the war..

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    On a side note take a look at your indentation too. Indent to three or four spaces each time, This can greatly increase program readability and greatly reduce debugging
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I think thi should be like:
    Code:
    if(*p1==*p2)   //;
    {
       sum++;
       p1++; 
       p2++;
    }
    else
    {
       break;
    }
    If you'll use breakets always even for the single line if/else it will be easier to spot such errors... maybe

    also in the WordLength function - you don't need i
    Just return p1-Word
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > while(*pTemp2 && isalpha(*pTemp2)==0)
    Your code would probably be easier to understand if this loop were in a separate function.

    > if(*p1==*p2);
    This trailing ; in wordcompare isn't doing any good either.
    But I see vart has already spotted that one
    Using lots of braces to make your intentions obvious is a good idea as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CODE Tags script bug
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 10-15-2006, 03:18 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Bug in Code
    By bigpmc in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2004, 05:51 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21