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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
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]<B[m]){
        if(A[n]==B[m]){
            code++;
        }
    }
 }


void Printing(const char CRfv, char A, int n, int code)
{

    ofstream fr(CRfv, ios::app);

    fr << code;
}
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>

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.
e.g. instead of having
1
2
3
4
char ch;
...
...
if (ch == 32)

put this instead,
if (ch == ' ')

or sometime it may be more legible to do something like this:
1
2
const char space = ' ';
if (ch == space)



Topic archived. No new replies allowed.