-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_check_21BCE3982.cpp
67 lines (55 loc) · 1.58 KB
/
string_check_21BCE3982.cpp
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
#include <bits/stdc++.h>
using namespace std;
bool lang2Check(const string& input) {
bool hasA = false;
bool hasB = false;
// Check the string
for (size_t i = 0; i < input.length(); ++i) {
if (input[i] == 'a') {
hasA = true;
} else if (input[i] == 'b') {
if (!hasA)
return false; // 'b' cannot appear before 'a'
hasB = true;
} else {
// Invalid character encountered
return false;
}
}
return hasA && hasB;
}
bool lang1Check(const string& s){
return s=="a";
}
bool lang3Check(const string& s){
return s=="abb";
}
int main(){
string lang1, lang2, lang3;
lang1 = "a";
lang2 = "a*b+";
lang3 = "abb";
cout<<"The languages are : "<<endl;
cout<<"1. "<<lang1<<endl;
cout<<"2. "<<lang2<<endl;
cout<<"3. "<<lang3<<endl;
ifstream fs;
fs.open(R"(C:\Users\khatu\github\CPPCode\string_check_file.txt)", ios::in);
if (!fs){
cout<<"File Not Found"<<endl;
return 0;
}
if (fs.is_open()) {
string line;
while (getline(fs, line)) {
auto len = line.length();
if (len == 0) continue;
if (lang1Check(line)) cout << line << " belongs to language 1" << endl;
else if (lang2Check(line)) cout << line << " belongs to language 2" << endl;
else if (lang3Check(line)) cout << line << " belongs to language 3" << endl;
else cout << line << " does not belong to any of the languages" << endl;
}
fs.close();
}
return 0;
}