understanding loop logic

Why this code is not working? I want to read in tagless all characters except the tags

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
#include<iostream>
#include<cstring>

using namespace std;
void parse(char[]);
int main(){

    char line[]="<name>Mohammad Rahman</name>";

    parse(line);
    return 0;
}

void parse(char line[]){

int len=strlen(line);
char tagless[40];
cout<<len<<endl;
for(int i=0;i<len;i++){

   if(line[i]=='<')
        while(line[i]!='>')
            continue;
    tagless[i]=line[i];


}
    cout<<tagless<<endl;

}
1
2
while(line[i]!='>')
    continue;


If the loop condition line[i]!='>' is true there is no way this loop will end because nothing (including the loop condition) changes within the loop.
thanks..How could I just print the name without the tags? Using loop is it possible?
Last edited on
Think about what happens after you skip the first tag: you want to move the 7th character of line to the 1st position in tagless. In other words, the index values for the two strings will be different. So your logic needs to be something like:
1
2
3
4
5
6
7
8
9
for each position i in line
    if (the i'th character in line is '<') {
        increment i until the i'th character in line is '>'
    } else {
        set j'th character if tagless to i'th character in line
        increment j
    }
}
set j'th character of tagless to 0 (to null-terminate it). 

Note a few things about this logic:
- It will work if you see adjacent tags <like><this>
- It will work if the string ends with a tag.
- It won't work for nested tags <like <this>>
- It won't work (and may crash) if a '<' doesn't have a matching '>'

I did this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void parse(char line[]){

int len=strlen(line);
char tagless[40];
cout<<len<<endl;
for(int i=0;i<len;i++){

    if(line[i]=='<'){
        i++;
        if(line[i]=='>')
            break;

        }

    else
        tagless[i]=line[i];


}
    cout<<tagless<<endl;

}


but output is coming

ame>Mohammad Rahman


also how can I add null at end given strlen can vary
The problem is lines 9-13. This appears to be where you're trying to do the part where I said "increment i until the i'th character is '>'."

So you actually need another loop here, not just an "if" statement.

Also tagless[i] = line[i] is wrong. The index for tagless will be different from the index for line. You need to keep track of both indexes. In my pseudocode I called them i and j.

You can add the null at the end of tagless the same way that you will add the other characters: tagless[j] = 0;
how tagless[i]=line[i] is wrong? Assume that at line[4] M starts so it will be assigning M to tagless[0]? sorry if I sound stupid
how tagless[i]=line[i] is wrong? Assume that at line[4] M starts so it will be assigning M to tagless[0]?

You use i as index to both arrays so if i == 4 this is what will happen: tagless[4] = line[4];
ohhh!
Topic archived. No new replies allowed.