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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <iostream>
using namespace std;
// Constants
// NULL character. This is the last char of all C Strings
const char END = '\0';
// Single quotation character '
const char SQUOTE = '\'';
// Double quotation character "
const char DQUOTE = '\"';
// Error. Used in Task 2 and 3
const int ERROR = -1;
// Practice Task: Task 0 (Not Graded)
unsigned int recursive_strlen(const char line[], int start)
{
if(line[start]!= END){
recursive_strlen(line,++start);
}
else if(line[start]== END){
return start;
}
}
// Normal Task: Task 1
unsigned int count_dquotes(const char line[], int start)
{
int x = recursive_strlen(line,0);
if(x>start){
if(line[start]== '"'){
if(start==(x-1)){return 1;}
else{return 1+count_dquotes(line,start+1);}
}
else{
if(start==(x-1)){return 0;}
else{return 0+count_dquotes(line,start+1);}
}
}
}
// Normal Task: Task 2
int find_first_dquote(const char line[], int start)
{
if(recursive_strlen(line,start)==0||count_dquotes(line,start)==0){return ERROR;}
else if(line[start]!= END){
if(line[start]=='"'){return start;}
else{find_first_dquote(line,start+1);}
}
}
// Normal Task: Task 3
int count_chars_in_matched_dquote(const char line[], int start)
{
int len = recursive_strlen(line,0);
if(start==0 && count_dquotes(line,start)%2==1 ){
return -1;
}
else if(start==0 &&count_dquotes(line,start)==0){
return 0;
}
else if(start<len && count_dquotes(line,start)!=0){
int x = find_first_dquote(line,start);
int y = find_first_dquote(line,1+x)-x-1;
return y + count_chars_in_matched_dquote(line,find_first_dquote(line,1+x)+1);
}
}
// Challenging Task: Task 4
bool check_quotes_matched(const char line[], int start)
{
}
// Challenging Task: Task 5
unsigned int length_of_longest_consecutive_dquotes(const char line[], int start)
{
}
// DO NOT WRITE ANYTHING AFTER THIS LINE. ANYTHING AFTER THIS LINE WILL BE REPLACED
const int MAX_LENGTH = 1000;
int main()
{
int option = 0;
char line[MAX_LENGTH];
do {
cout << "Options:" << endl;
cout << "0: Test recursive_strlen()" << endl;
cout << "1: Test count_dquotes()" << endl;
cout << "2: Test find_first_dquote()" << endl;
cout << "3: Test count_chars_in_matched_dquote()" << endl;
cout << "4: Test check_quotes_matched()" << endl;
cout << "5: Test length_of_longest_consecutive_dquotes()" << endl;
cout << "-1: Quit" << endl;
cin >> option;
cin.ignore();
switch (option) {
case 0:
cout << "Testing recursive_strlen()" << endl;
cout << "Enter line: ";
cin.getline(line, MAX_LENGTH);
cout << recursive_strlen(line, 0) << endl;
break;
case 1:
cout << "Testing count_dquotes()" << endl;
cout << "Enter line: ";
cin.getline(line, MAX_LENGTH);
cout << count_dquotes(line, 0) << endl;
break;
case 2:
cout << "Testing find_first_dquote()" << endl;
cout << "Enter line: ";
cin.getline(line, MAX_LENGTH);
cout << find_first_dquote(line, 0) << endl;
break;
case 3:
cout << "Testing count_chars_in_matched_dquote()" << endl;
cout << "Enter line: ";
cin.getline(line, MAX_LENGTH);
cout << count_chars_in_matched_dquote(line, 0) << endl;
break;
case 4:
cout << "Testing check_quotes_matched()" << endl;
cout << "Enter line: ";
cin.getline(line, MAX_LENGTH);
cout << check_quotes_matched(line, 0) << endl;
break;
case 5:
cout << "Testing length_of_longest_consecutive_dquotes()" << endl;
cout << "Enter line: ";
cin.getline(line, MAX_LENGTH);
cout << length_of_longest_consecutive_dquotes(line, 0) << endl;
break;
default:
break;
}
cout << endl;
} while (option != -1);
return 0;
}
|