Can someone explain to me my teacher's code?

Hello guys,
I recently started learning to program in C++ and I have hard time understanding my teacher's code. So I understand a big part of the code but what I don't understand is the content of the lines 11 to 13. I'm familiar with the loops etc. but I can't understand the point of the both While loops.
And another thing I want to ask is : what if(*adr) means ? Any help would be appreciated ;)


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
#include<iostream>
using namespace std;
#include<cstring>
int main() {
	const short maxlen = 30;
	char str[maxlen + 1], sub[maxlen+1];
	cout << "String<maxlenght <= " << maxlen << " > ";
	cin.getline(str, maxlen + 1);
	cout << "Sequence<maxlenght <= " << maxlen << " > ";
	cin.getline(sub, maxlen + 1);
	char *adr = sub, *next = str;
	while (*adr) {
		if ((next =  strchr(next, *adr)) == NULL) break ;
		else {
			++next;
			++adr;
		}

	}
if (*adr) cout << "No" << endl; // Is if(*adr) the same as the if(*adr == NULL)
	else cout << "Yes\n";

	short pos[maxlen], count = 0;
	adr = sub;
	next = str;

	while (*adr) {
		if ((next = strchr(next, *adr)) == NULL) break;
		else {
			pos[count++] = next - str;
			++next;
			++adr;
			}
} // ?? What is the point of this while loop ??
	if (*adr) cout << "No" << endl;
	else {
		for (short i = 0; i < count; i++) cout << pos[i] << " ";
		cout << endl;
	}
	system("pause");
	return 0;
}
Hello Cskarch96!

Lines 11 to 13 deal with a pointer (adr) that points to the character array sub. Also another pointer (next) points to the character array str.

The "while" loop that starts on line 12 is going through each character in each array and comparing them.

Line 20 is a very common comparison that is explained by your teacher's comment. It says
if the character that adr is pointing to is NOT NULL, then output the word "No".
Thank you for your help!
Topic archived. No new replies allowed.