### comparing two words

Hello everyone, I have been struggling with this one for days. It is my homework assignment and it is due tomorrow. I know I won't make it but I'm looking for some words of wisdom for my future programs.

The main idea of the task is that you have two words 'Arklys' and 'Atvykstama'. what you must do is compare 'Atvykstama' to 'Arklys' in the matter of letter position. The first letter of 'Arklys' is 0, second - 1, and so on. To my way of thinking, it must be done this way: the first letter of 'Atvykstama' is 'A', and so is on 'Arklys', in that way, you get 0, then, we find 'Y' which is the fourth(fifth) on 'Arklys'.. I hope you get the idea. While comparing if a letter is found that isn't present on both words it is skipped. In the end you must get an answer in numbers, that is the decoded code: 042500.

These words are in Lithuanian so I am sorry, if they confuse you. This is what I've done so far:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include #include using namespace std; const char CDfv1[]="data1.txt"; // this is where 'Arklys' is placed const char CDfv2[]="data2.txt"; // this is where 'Atvykstama' is placed const char CRfv[]="results.txt"; const int CMax = 250; void ReadingFromFile(const char CDfv, char A, int & n); void Decode( char A, char B, int n, int m, int & code); void Printing(const char CRfv, char A, int n, int code); int main() { char A[CMax]; int n; char B[CMax]; int m; int code; ReadingFromFile(CDfv1, A, n); ReadingFromFile(CDfv2, B, m); Decode(A, B, n, m, code); ofstream fr(CRfv); fr.close(); Printing(CRfv, A, n, code); Printing(CRfv, B, m, code); fr.close(); return 0; } void ReadingFromFile(const char CDfv, char A, int & n) { char s; n=0; ifstream fd(CDfv); while(!fd.eof()){ fd.get(s); A[n]=tolower(s); n++; } } void Decoding( char A, char B, int n, int m, int & code) { kodas = 0; while(A[n]
Last edited on
Please put the code in code format. Also in your functions remove the `[]` from the char variables. EX: `void Printing(const char CRfv[], char A[], int n, int code)` should be `void Printing(const char CRfv, char A, int n, int code)`
Last edited on
done :) so has anyone have some about making this work?
This has only been tested for a few cases. However, it seemed to work for yours. Try it at your own risk.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include #include using namespace std; const int arrayMax = 250; int absValue(int v) { if(v < 0) return (v*-1); else return v; } int main() { std::string file1 = "data1.txt"; std::string word1; std::string file2 = "data2.txt"; std::string word2; int counter = 0; fstream in1; in1.open(file1); fstream in2; in2.open(file2); ofstream out; out.open("results.txt"); in1 >> word1; in2 >> word2; while((in1)&&(in2)) { std::string temp = ""; // *Initialize empty for(int i = 0; i < word2.size(); ++i) for(int j = 0; j < word1.size(); ++j) if((word2[i] == word1[j])||(absValue(word2[i]-word1[j]) == 32)) // **See online ASCII tables for integer values temp += ('0'+j); // *Because of this statement out << temp << endl; counter++; in1 >> word1; in2 >> word2; } return 0; }``````
Thanks it really did work! But I did not catch the part with ASCII tables. Why 32? in here http://www.asciitable.com/ it does not mention letters until 97 or so..
32 is ASCII code for space.
You can avoid having to memorise the entire ASCII table by using ' ' instead.
 ``1234`` ``````char ch; ... ... if (ch == 32)``````

`if (ch == ' ')`
 ``12`` ``````const char space = ' '; if (ch == space)``````