Function not being invoked

Mar 30, 2017 at 7:11pm
my function isnt being invoked so it just compiles and ends, any idea's?

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 #include<iostream>
#include<string>
#include<ctime>
using namespace std;

// func proto
// bool isAnagram(string &s1, string &s2);
bool isAnagram(const string s1, const string s2);



int main()
{
	// declare the variables 
	string i1;

	string i2;
	// read the input for string1 and string 2	
	// display the output
	cout << "Enter a string s1: " << endl;
	cin >> i1;
	cout << "\nEnter a string s2: " << endl;
	cin >> i2;
	// invoke the func 
	isAnagram(i1, i2);

	system("pause");

	return 0;
}


bool isAnagram(const string s1, const string s2)
{
	// variable  dec 
	int str1;
	int str2;
	int count;
	count = 0;

	bool isAnagram = true;

	// define the size 

	str1 = s1.size();

	str2 = s2.size();

	// compare both the strings 

	if (str1 != str2)
	{
		cout << "\n The strings are not anagram" << endl;
		return 0;
	}

	for (int i = str2 - 1; i >= 0; i--)
	{
		if (s1[count] != s2[i])
		{
			count++;

			continue;
		}
		else
		{
			// display message
			cout << "\nSorry, not an anagram" << endl;
			isAnagram = false;

			return 0;
		}// endl loop

		if (isAnagram)
		{
			// display out put
			cout << "\nAnagrams" << endl;

		}
	}
}
Mar 30, 2017 at 7:25pm
Just ran the program as is and the function worked was called just fine
Mar 30, 2017 at 8:36pm
Your function was called (try strings of different length) but your method won't work, particularly if there are repeated letters.

I should sort the two strings (use std::sort()) and then compare for equality.

You might want to deal with the upper-case/lower-case issue as well.
Last edited on Mar 30, 2017 at 8:39pm
Topic archived. No new replies allowed.