0

hi everybody!...
so i have this assigment for monday and im having trouble with it... the program in c should get:

input:
a sentence.....nanny do you have any cheap peach...

and it should print the "same" words...:
nanny any
cheap peach

..they are called "same words when they get the same letter no matter upper or lower case and how many time each letter....

so this is my code i have no idea whats wrong... but can somebody help me fixe it????


thank you alll!

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

char wordA(char sentence[80], int x);
char wordB(char sentence[80], int x);
int endwordA(char sentence[80], int x);
int endwordB(char sentence[80], int x);
int checkword(char word1[80]);

int main()
{
char sentence[80], word1[80], word2[80];
int x=0, endword1=0, endword2=0, counter1, counter2, endwordimp;

// receives the input from the user
printf("Please enter a sentence\n");
gets(sentence);

//running the sentence
while (sentence[x]!='\0')
{
	int i=0;
	int w=1;
		//separating the first word.....
		word1[x]= wordA(sentence, x);
		endword1= endwordA(sentence, x);
		endwordimp = endword1;
		while (i!=w)
		{
			// searching for the second word....
			word2[x]= wordB(sentence, endword1);
			endword2= endwordB(sentence, endword1);

			//cheking the letters of each word....//....returns the real value of each letter in the word!...
			counter1= checkword(word1);
			counter2= checkword(word2);

			// comparing the words...
			if (counter1==counter2)
			{
				printf ("%s %s", word1, word2);
				endword1= endword2;
				if (sentence[endword2]=='\0' || sentence[endword2+1]=='\0')
				{
					i=w;
					sentence[x]= sentence[endwordimp];
				}
			}
			else 
			{
				endword1= endword2;
				if (sentence[endword2]=='\0' || sentence[endword2+1]=='\0')
				{
					i=w;
					sentence[x]= sentence[endwordimp];
				}
			}
	}
}

system("PAUSE");
return 0;
}


// FUNCTION THAT CHECKS THE WORDS
int checkword(char word1[80])
{
	int counter=0, x=0;
	for (x; word1!='\0'; x++)
	{
		if ((word1[x]>=65) && (word1[x]<=90))
		{
		counter= (int)word1[x];
		}
		else
		{
		counter= (int)word1[x] - 32;
		}
	}
return (counter);
}




///// FUNCTION THAT CREATE ME THE FIRST WORD
char wordA(char sentence[80], int x)
{
	char  word1[80];
    int y=0;
for (x; x<=strlen(sentence) ; x++)
       {
		   while ((sentence[x]>122 || sentence[x]<97) && (sentence[x]>90 || sentence[x]<65))
			{
               x++;
			}
for (x; x<=strlen(sentence); x++)
			{
			if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
				{
				word1[y]=sentence[x];
				y++;
				}
            else
				{
				word1[y]='\0';
				x++;
				break;
				}
		break;
	}
 break;
}
return (word1);
}

/////////////////// functions that takes me to the end of the first word!
int endwordA(char sentence[80], int x)
{
    char word1[80];
	int y=0;
for (x; x<=strlen(sentence) ; x++)
       {
		 if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
			{
			word1[y]=sentence[x];
			y++;
            }
            else
	        {
	         word1[y]='\0';
	         x++;
	         break;
	         }
	}
return (x);
}

/// FUNCTINOS THAT GIVES ME THE SECOND WORD... (WORD TO COMPARE)
char wordB(char sentence[80], int endword1)
{
	int y=0;
	char word2[80];
	int x=endword1;
for (x; x<=strlen(sentence) ; x++)
       {
         while ((sentence[x]>122 || sentence[x]<97) && (sentence[x]>90 || sentence[x]<65))
			{
               x++;
			}
         for (x; x<=strlen(sentence); x++)
			{
			if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
				{
				word2[y]=sentence[x];
				y++;
				}
            else
				{
				word2[y]='\0';
				x++;
				break;
				}
			}
		break;
		}
return (word2[80]);
}

/////// FUNCTION THAT TAKES ME TO THE END OF THE SECOND WORD.......
int endwordB(char sentence[80], int endword1)
{
	char word1[80];
    int y=0;
	int x=endword1;
for (x; x<=strlen(sentence) ; x++)
       {
		 if ((sentence[x]>=97 && sentence[x]<=122) || (sentence[x]>=65 && sentence[x]<=90))
			{
			word1[y]=sentence[x];
			y++;
            }
            else
	        {
	         word1[y]='\0';
	         x++;
	         break;
	         }
	}
return (x);
}

thanx!

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Murtan
0

When asking for help, please describe the symptoms you do see (if any).

For debugging purposes, you should add some print statements to your code to confirm that you are getting the data you expect, where you expect it and for progress messages so you can tell where you stopped at if your program doesn't complete.

If this was c++ code it wouldn't compile (the return value on line 115 does not match the specified return type on line 88)

Your wordA and endWordA functions do not quite use the same algorithm.

The for statement on line 70 in checkword will likely cause an infinite loop. The test condition should be for word1[x] even if you fixed that, the function does not do what you need it to do. It needs to form a representation of the letters that are in the word, regardless of order or frequency. The algorithm you have works for order, but not for frequency.

If you're not attached to them, changing the prototype for wordA might allow you to actually do what you're attempting and return where the word ended at the same time.

int nextWord(char sentence[80], int startx, char wordfound[80]);

Where it will return where the word ended and fill the wordfound array with the word it finds.


A 'better' overall algorithm might be:

  • force the sentence to upper or lower case
    I don't see where you have to preserve the case of the input
  • split the sentence into words, keeping all of the words
    you might look at strtok(), strspn() and strcspn() for help splitting
    you will probably need an array of words
  • "score" the words
    you might consider a bit-based algorithm
  • now compare the scores of all the words

The above has the advantage of having intermediate 'deliverables' that you could output and verify to confirm that things are working as you expect them to.

If you don't want to change algorithms mid-stream, I understand, I'll still try to help you make what you're trying to do work. But you've still got a lot of work to go on what you've started.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.