is symmetric to a dollar sign?

Sep 8, 2019 at 7:48pm
Tips, advice, suggestions on how to do make symmetric to the dollar sign
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
#include <iostream>
#include <string>
#include "Stack.h"
using namespace std;
bool isInLanguage_1(string aString)
{
 Stack<char> chStack;
 int length = aString.length();
 int index_of_middle = length / 2;
 if (aString.empty())
 {
  cout << "Your string is empty. Program will terminate." << endl;
  return false;
 }
 if (length == 1 && aString[0] == '$')
 {
  cout << "Your string is in language." << endl;
  return true;
 }
 if (length % 2 == 0)
 {
  cout << "Your string is even, therefore it cannot be symetric to $." << endl;
  return false;
 }
 if (length % 2 != 0 && aString[index_of_middle] == '$')
 {
  for (int a = 0; a < index_of_middle; a++)
  {
   //char b = aString[a];
   chStack.push(aString[a]);
  }
  for (int a = index_of_middle + 1; a < length; a++)
  {
   if (aString[a] == chStack.peek())
   {
    chStack.pop();
   }
  }
  chStack.display();
 }
}
int main()
{
 string aString;
 cout << "Please enter a string of characters with no spaces." << endl;
 cin >> aString;
 isInLanguage_1(aString);


 system("pause");
 return 0;
}
Sep 8, 2019 at 8:01pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

bool isSymm( string aString, char centre )
{
   int N = aString.size();   if ( !(N % 2) || aString[N/2] != centre ) return false;
   return aString == string( aString.rbegin(), aString.rend() );
}

int main()
{
   const char c = '$';
   string aString;
   cout << "Please enter a string of characters: ";   getline( cin, aString );
   cout << aString << ( isSymm( aString, c ) ? " is " : " is not " ) << "symmetric with respect to " << c << '\n';
}
Sep 8, 2019 at 9:55pm
thanks for the idea involving const char c = '$'
and the conditional expression ?:

if you ever have time to explain anything, I'm all ears or eyes, meaning I will for sure read everything you have to say. Thank You for the reply.
Topic archived. No new replies allowed.