aboutsummaryrefslogtreecommitdiff
path: root/11c/10.15.1/10.15.1.cpp
blob: 2228f7a94906590d91d1c81edc086eb98bd8cbad (plain) (blame)
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
83
84
// 10.15.1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(char str[50]);

bool isAlphaStr(char str[50]);

int countChar(char str[50], char c);

int main()
{
    char str[50]{};
    char check;
    int count;
    
    cout << "Input a string: ";
    cin.getline(str, 50);
    cout << "Input a charcter to check: ";
    cin >> check;
    cout << "\n\n";

    if (isPalindrome(str))
    {
        cout << "\"" << str << "\" is a palindrome.\n";
    }
    else
    {
        cout << "\"" << str << "\" is not a palindrome.\n";
    }

    if (isAlphaStr(str))
    {
        cout << "\"" << str << "\" only contains alpahbetic charcters.\n";
    }
    else
    {
        cout << "\"" << str << "\" contains one or more non alphabetic charcters.\n";
    }

    count = countChar(str, check);
    cout << "\"" << str << "\" contains " << count << " " << check << "'s.";
}

bool isPalindrome(char str[50])
{  
    string s = str, p = s;
    reverse(p.begin(), p.end());

    return s == p;
}

bool isAlphaStr(char str[50])
{
    int i = 0;
    while (str[i])
    {
        if (!isalpha(str[i]))
        {
            return false;
        }
        i++;
    }
    return true;
}

int countChar(char str[50], char c)
{
    int count = 0, i = 0;
    while (str[i])
    {
        if (str[i] == c)
        {
            count++;
        }
        i++;
    }
    return count;
}