C
Chris R.
Hi everyone. I am trying to finish my homework, but I seem not to
figure out how to fix this structure that seems to make wrong output.
What this program should do is use structure to save the word and line
number on which it was in array pointer using malloc and print them
out alphabetized (no duplicates).
Most of it works, except the words seem to be corrupted. Here is a
code, if anyone can help me it would help so much since I've been
working on this for a really long time. I tried even changing '\0' to
NULL, but then program errored out.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct list
{
int linenum;
char word[30];
} LIST;
void main()
{
LIST *p[1000], *holder;
int line = 1;
int pcounter = 0;
char buffer[40];
int buffercounter = 0;
char letter = ' ';
int inword = 0;
int i;
int compare;
int shouldstore = 1;
int x,y;
while (letter != EOF)
{
letter = getchar();
if ( (letter >= 'a' && letter <= 'z') || (letter >=
'A' && letter <= 'Z') )
{
letter = toupper(letter);
inword = 1;
buffer[buffercounter] = letter;
buffercounter++;
}
else if (letter == '\n')
{
line++;
if (inword == 1)
{
inword = 0;
buffercounter++;
buffer[buffercounter] = '\0';
buffercounter = 0;
if (pcounter == 0)
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word , buffer);
p[pcounter]->linenum = line;
pcounter++;
}
else
{
for (i = 0; i < pcounter; i++)
{
compare = strcmp(p->word, buffer);
if (compare == 0)
{
shouldstore = 0;
}
}
if (shouldstore == 0)
{
shouldstore = 1;
}
else
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word, buffer);
p[pcounter]->linenum = line;
pcounter++;
}
}
}
else
{
buffercounter = 0;
}
}
else
{
if (inword == 1)
{
inword = 0;
buffercounter++;
buffer[buffercounter] = '\0';
if (pcounter == 0)
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word , buffer);
p[pcounter]->linenum = line;
pcounter++;
}
else
{
for (i = 0; i < pcounter; i++)
{
compare = strcmp(p->word, buffer);
if (compare == 0)
{
shouldstore = 0;
}
}
if (shouldstore == 0)
{
shouldstore = 1;
}
else
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word, buffer);
p[pcounter]->linenum = line;
pcounter++;
}
}
}
else
{
buffercounter = 0;
}
}
}
for(x = 0; x < pcounter; x++)
for(y = 0; y < pcounter-1; y++)
{
compare = strcmp(p[y]->word, p[y+1]->word);
if( compare < 0)
{
holder = p[y+1];
p[y+1] = p[y];
p[y] = holder;
}
}
for (x = 0; x < pcounter; x++)
{
printf("\nThe word %s was on line %d", p[x]->word, p[x]->linenum);
}
}
figure out how to fix this structure that seems to make wrong output.
What this program should do is use structure to save the word and line
number on which it was in array pointer using malloc and print them
out alphabetized (no duplicates).
Most of it works, except the words seem to be corrupted. Here is a
code, if anyone can help me it would help so much since I've been
working on this for a really long time. I tried even changing '\0' to
NULL, but then program errored out.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct list
{
int linenum;
char word[30];
} LIST;
void main()
{
LIST *p[1000], *holder;
int line = 1;
int pcounter = 0;
char buffer[40];
int buffercounter = 0;
char letter = ' ';
int inword = 0;
int i;
int compare;
int shouldstore = 1;
int x,y;
while (letter != EOF)
{
letter = getchar();
if ( (letter >= 'a' && letter <= 'z') || (letter >=
'A' && letter <= 'Z') )
{
letter = toupper(letter);
inword = 1;
buffer[buffercounter] = letter;
buffercounter++;
}
else if (letter == '\n')
{
line++;
if (inword == 1)
{
inword = 0;
buffercounter++;
buffer[buffercounter] = '\0';
buffercounter = 0;
if (pcounter == 0)
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word , buffer);
p[pcounter]->linenum = line;
pcounter++;
}
else
{
for (i = 0; i < pcounter; i++)
{
compare = strcmp(p->word, buffer);
if (compare == 0)
{
shouldstore = 0;
}
}
if (shouldstore == 0)
{
shouldstore = 1;
}
else
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word, buffer);
p[pcounter]->linenum = line;
pcounter++;
}
}
}
else
{
buffercounter = 0;
}
}
else
{
if (inword == 1)
{
inword = 0;
buffercounter++;
buffer[buffercounter] = '\0';
if (pcounter == 0)
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word , buffer);
p[pcounter]->linenum = line;
pcounter++;
}
else
{
for (i = 0; i < pcounter; i++)
{
compare = strcmp(p->word, buffer);
if (compare == 0)
{
shouldstore = 0;
}
}
if (shouldstore == 0)
{
shouldstore = 1;
}
else
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word, buffer);
p[pcounter]->linenum = line;
pcounter++;
}
}
}
else
{
buffercounter = 0;
}
}
}
for(x = 0; x < pcounter; x++)
for(y = 0; y < pcounter-1; y++)
{
compare = strcmp(p[y]->word, p[y+1]->word);
if( compare < 0)
{
holder = p[y+1];
p[y+1] = p[y];
p[y] = holder;
}
}
for (x = 0; x < pcounter; x++)
{
printf("\nThe word %s was on line %d", p[x]->word, p[x]->linenum);
}
}