use the recursion only

I have been thinking about task 4 and 5 for many days, but it has not solved the problem, can you help me?
the question of Task 4
https://imgur.com/OBZ1WV3

the question of Task 5
https://imgur.com/D3or4W9

* Note:
* - DO NOT change any of the function headers given
* - DO NOT use any loops
* - DO NOT use any global variables or add additional libraries.
* - You can add helper function(s) if needed.
*/
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;
}
how do you count quotes recursively?
if its not the end of the string, call yourself.
after calling yourself, return a 1 or a 0 if its a quote or not.
you are taking a running sum of that, a static variable is one way to do that (remember to reset it to zero when doing a new block of text!).
if its an even#, your quotes matched (?) right?
OBZ1WV3:

Challenging Tasks 
Task 4
bool check_quotes_matched(const char line[], int start)
- This function checks whether quotation characters (single and double quotation
  characters) in the string (line) are matched.
- Return:
    - true if all quotation characters are matched, or no quotation characters
      are found.
    - false otherwise.

Hint
- Write two helper functions to handle:
    1. When a single quotation character is read.
    2. When a double quotation character is read.
- These three functions should call each other.

| Sample Input (line)  | Return Value  |
|----------------------|---------------|
| (Empty)              |     true      |
| COMP2011             |     true      |
| 'COMP2011'           |     true      |
| Hello " World        |     false     |
|  " 'Hello World' "   |     true      |
| "A single quote: '"  |     true      |
| "A'A'A'A"            |     true      |
| '"A""A"              |     false     |


D3or4W9:

Task 5
unsigned int length_of_longest_consecutive_dquotes(const char line[], int start)
- This function finds the length of the longest consecutive sequence of double
  quotation characters in the string (line).
• It returns the length of the longest consecutive sequence of double quotation
  characters.

Hint
- Write a recursive helper function that takes additional parameters.

|Sample Input (line)  | Return Value |
|---------------------|--------------|
|(Empty)              |      0       |
| COMP2011            |      0       |
| "COMP2011"          |      1       |
| Hello " World       |      1       |
| """Hello World"""   |      3       |
| AAAAAA""BBB         |      2       |
| """AAA""""""        |      6       |
| """""BBBBBBBBB"""   |      5       |


Wouldn’t it have been simpler to just link to the webpages where these exercises come from?
What kind of help do you want?
Task 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool check_quotes_matched(char const* s);

bool in_quote(char const* s, char q)
{ 
  return (*s == q   )? check_quotes_matched(s + 1)
       : (*s == '\0')? false : in_quote(s + 1, q);
}

bool check_quotes_matched(char const* s)
{
  return (*s == '"' )? in_quote(s + 1, '"' )
       : (*s == '\'')? in_quote(s + 1, '\'')
       : (*s == '\0')? true : check_quotes_matched(s + 1);
}
Last edited on
That's pretty good, but I assumed it was supposed to be able to handle nested quotes, like:

one "two 'three' four" five

I assumed it was supposed to be able to handle nested quotes
I thought so myself, at first, but there's this test case:
| "A'A'A'A" | true |
Last edited on
Topic archived. No new replies allowed.