-
problem on assignment
hi guys:
I got a problem on my assignment. On my assignment program, it needs to find out the open tag and close tag of html string and find the index of the string within open tag and close tag.
I also start my program with following code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int getPoints(char html[], char tagname[], int tagnum, int *start, int
*end);
int getPoints(char html[], char tagname[], int tagnum, int *start, int
*end)
{
int track;
int index;
int words;
int i;
int j;
int k;
int a;
int b;
int get;
int put;
track = strlen(html);
for (j=0; j<track; j++)
{
html[j] = toupper(html[j]);
}
printf("%s\n",html);
for(b=0; b<track; b++)
{
tagname[b] = toupper(tagname[b]);
}
printf("%s\n",tagname);
index = strlen(tagname);
put = 0;
for (i=0; i < track; i++)
{
if (html[i] == '<' && (html[i+index] = tagname[i]) &&
html[i+index+1] == '>')
{
if (put != tagnum)
put++;
if (put = tagnum)
*start = i + index + 2;
}
else
{
*start = 0;
words = 0;
return words;
}
}
get = put;
for (a=0; a>track; a++)
{
if(html[a] == '<' && html[a+1] == '/' && (html[a+1+index] =
tagname[a]) && html[a+2+index] == '>')
{
if (get != tagnum)
{
get--;
}
if (get = tagnum)
{
*end = a - 1;
}
}
else
{
*end = 0;
words = 0;
return words;
}
}
words = *end - *start + 1;
return words;
}
void main()
{
int r;
int s;
int e;
r=getPoints("what the <P> hfasbgka</P>fkkhgwag<p>sdjhfkjhjkasg</p>",
"p",1,&s,&e);
}
I compile on my school AIX platform, it's runned but the compiler showed the statement Segmentational Fault(coredump). I don't know what's wrong on my program and which parts of my program i need to fix
-
Because you're trying to modify a string constant
> "what the <P> hfasbgka</P>fkkhgwag<p>sdjhfkjhjkasg</p>"
This is stored in read only memory
> html[j] = toupper(html[j]);
And this tries to modify it, which is a no-no
Try this
Code:
int main ( ) { // yes, main returns int - ALWAYS
char test[] = "what the <P> hfasbgka</P>fkkhgwag<p>sdjhfkjhjkasg</p>";
int r,s,e;
r=getPoints( test, "p",1,&s,&e);
return 0;
}