| | |
array help! please
![]() |
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 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!
thanx!
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!
C Syntax (Toggle Plain Text)
#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!
•
•
Join Date: May 2008
Posts: 662
Reputation:
Solved Threads: 112
0
#2 Dec 11th, 2009
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
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.
Where it will return where the word ended and fill the wordfound array with the word it finds.
A 'better' overall algorithm might be:
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.
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.
c Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- How can is see what button is pressed in a button array (VB.NET)
- Printing a histogram from a frequency array (C)
- troubleshoot char array; (C++)
- Dynamic array => dropdown issues... (PHP)
- passing a pointer/reference to array into a class (C#)
Other Threads in the C Forum
- Previous Thread: DEQUE Program!
- Next Thread: program to block unwanted websites in internet explorer and allow few website
Views: 304 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include api array arrays binary binarysearch bit c++ calling char character code coke command conversion convert database decimal dice directory directorystructure do-not-use drawing dude dynamic error exec executable factorial fgets file fork framework free function functions givemetehcode givemetehcodez grade graphics gtkgcurlcompiling haiku hangman help|help|help|help homework i/o input insert int integer intmain() lazy line linked linkedlist linux list lists loop lowest malloc matrix memory mysql no-effort output parallel path permutations pointer pointers problem process profile program programming read recursion recursive recv reverse scanf segmentationfault socketprograming spoonfeeding stack string strings strtok structures student system testing turbo-c turboc unix user variable windows _getdelim





