akohan
asked on
dealing with upercase and lowercase in C
hello group,
In the following snippet, I have to deal with different cases of html tags either written in lower case or upper case. For instance, the title could be:
<title>text goes here</title>
or
<TITLE>text goes here</TITLE>
in following approach only the lower case will be located. what is the best approach to get both of them? since there could be a situation such as Title or tItle... and so on.
In the following snippet, I have to deal with different cases of html tags either written in lower case or upper case. For instance, the title could be:
<title>text goes here</title>
or
<TITLE>text goes here</TITLE>
in following approach only the lower case will be located. what is the best approach to get both of them? since there could be a situation such as Title or tItle... and so on.
char* beginning_tag_title = "<title>";
char* ending_tag_title = "</title>";
char* p_start = NULL;
char* p_end = NULL;
p_start = strstr(buf, beginning_tag_title);
p_end = strstr(buf, ending_tag_title);
just convert your buffer to lower case before evaluating:
strlwr(buf);
strlwr(buf);
hmm, I see you will need to preserve the inner text case, so, if you don't want to make all the string lower, you can have a cloned buffer, but in lower case. So, when you want to locate the tags, you will deal with lower cases at the cloned buffer, if you want to extract the inner text just do it in the original buffer.
strcpy(buf1, buf); // buf1 should have the proper memory assignment, just as buf
strlwr(buf1);
now, when looking for tags, do it at buf1, when looking for inner texts, do it at buf. Tags are in the same position in both buffers.
strcpy(buf1, buf); // buf1 should have the proper memory assignment, just as buf
strlwr(buf1);
now, when looking for tags, do it at buf1, when looking for inner texts, do it at buf. Tags are in the same position in both buffers.
compare two strings ignoring case - strcascmp() - works exactly the same as strcmp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Another idea is for you to write your own special version of strstr that does a case insensitive search.