Link to home
Start Free TrialLog in
Avatar of Vasconcelos
Vasconcelos

asked on

stack peep problem

Good morning :)

My problem is: i want to peep the stack to find repeated words but im stuck and i dont know what i should do or what im doing wrong, btw everything else works except for the peep function.

best regards Vasconcelos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCOLUMS 100
#define MAXLINES 30
#define SIZE 128
 
char pilha[MAXLINES][MAXCOLUMS];
int top = 0;
char str2[] = "fim";
char str[SIZE];
char str1[SIZE];
 
 
void reads(char s[], int n)
{
    fgets(s, n, stdin);
    s[strlen(s) - 1] = '\0';
}
 
void push(char* str, int t)
{
    int i = 0;
    if(top == MAXLINES)
    {
        printf("\nSTACK OVERFLOW");
        return;
        }else{
            for(i = 0; i < t; i++)
            {
                pilha[top][i] = str[i];
            }
    }
}
 
 
char* pop()
{
    int i = 0;
    if(top == -1)
    {
        printf("\nSTACK UNDERFLOW");
    }else{
        return(pilha[top]);
    }
}
 
 
void separa_str(char str[]) {
    int i = 0;
    char *palavra = NULL;
    palavra = strtok(str, " ");
 
    while (palavra !=0) {
        i = strlen(palavra);
        push(palavra, i);
        top++;
        palavra = strtok( NULL, " " );
    }
}
 
void read_str()
{
    reads(str, SIZE);
    strcat(str, " ");
    do {
        printf("\n word to terminate?(fim)");
        reads(str1, SIZE);
 
        if (strcmp(str1, str2)!= 0)
            printf("\n wrong word try again..\n");
        else
            separa_str(str);
    } while (strcmp(str1, str2) != 0);
 
}
 
 
void p_print() {
    int i = 0;
    int j = 0;
    while(i < top){
        for (j = 0; j < MAXCOLUMS; j++) {
            printf("%c", pilha[i][j]);
        }
        printf("\n");
        i++;
    }
}
 
int peep()
{
    int i = 0;
    int j = 0;
    int a = 0;
    int b = 0;
    for(i = 0; i < top; i++)
        for(j = 0; j <MAXCOLUMS; j++)
            for(a = 0; a < top; a++)
                for(b = 0; b < MAXCOLUMS; b++)
                        if((strchr(pilha[top + a][i], pilha[top][i + b])) != 0)
                            return top;
}
 
 
 
main()
{
    int x = 0;
    char texto[SIZE];
    int tam = 0;
    printf("\n write whe text: ");
    read_str();
    p_print();
    peep();
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

What do you want your peep function to do ?

It performs some strange indexing into the array, with potentially out-of-bounds accesses.
Avatar of Vasconcelos
Vasconcelos

ASKER

i want it to return the position of the words repeated
What do you mean by words repeated ? A word that is on the stack more than once ? Any word ? Or a specific word ?
a word that that is on the stack more than once
Any word ?

Can you sort the stack ? That would make things a lot easier, and definitely faster.
yes any word inside the stack that is repeated :)

you mean like pushing the words on a specific order so it can facilitate the search?
I mean sorting the stack any way possible. Either the moment you push them onto the stack, or as part of the peep function, perform a sort operation on the stack first.

Note that this changes the semantics of the stack data structure, but then again, you're already performing operations on the stack that aren't really supposed to be done ...

A stack is a LIFO (last-in-first-out) structure, where the only access happens at the top of the stack (either pushing onto or popping from it) - the rest of the stack is never touched.
Which makes me wonder - are you sure you need a stack ? Wouldn't a different data structure be more appropriate ? What kind of operations do you need to be able to do on the data structure ?
Just expanding Infinitys comment:

(I)

>>         i = strlen(palavra);
>>        push(palavra, i);

You dont copy the terminating zero-byte of the string, because strlen only returns the number of characters excluding the zero byte, so it should be:

push(palavra, i+1);

because otherwise the potentially out-of-bounds accesses mentioned by Infinity can be here:

in pop() you return a char-pointer to pilha[top], following calls using it, like strlen or printf expect a terminating zero byte, otherwise they read out of bounds


(II)
in void push(char* str, int t), you dont check if the string is longer than MAXCOLUMS, also a potentially out of bounds access


ike
The operations i want to do are search for the place on the stack where a repeated word is and pop the word pointed by the peep function (that is if its possible) :)

a diferente data structure you mean like a queue?

Also thanks to ikework it was a very helpfull comment.
void reads(char s[], int n)
{
    fgets(s, n, stdin);
    s[strlen(s) - 1] = '\0';
}

shouldnt this be:

    s[n - 1] = '\0';
hmm isnt it correct the way it is? or will it give some unexpected error further in the code?
>> hmm isnt it correct the way it is? or will it give some unexpected error further in the code?

Well it makes sure there is a terminating zero, at least at end of buffer, but anyway, I think infinitys comment here
http:#a25643209
was much more important, you should focus on that first.
Could you respond to http:#25643205 and http:#25643209 please ? There's crucial information we need in order to assist you.
well im using a stack but if you can advise me on a more efficient data structure i would appreciate it :)
 the operations i want to do are, push some words on a stack peep the stack if any of the words are repeated and pop the repeated words, for example:

Inside the Stack:
one
for
all
and
all
for
one

new Stack:
one
for
all
and

i think i made it clear now :) btw if there are other methods to do this pls tell me so i can test them out
What will the data be used for ? Why did you originally choose a stack as data type ?

Up till now, an std::set sounds more appropriate :

        http://www.cplusplus.com/reference/stl/set/

It automatically avoids duplicates for you, and you can insert elements into it, and erase elements from it.
>> Why did you originally choose a stack as data type ?

Or in other words, do you need the data structure to be LIFO ?
Well i would like to use LIFO data structure:)
well im asking all this for the purpose of using the stack is for doing something like encrypting a text using ONLY the repeated words  in the process. So for that i need to know wich words are repeated.
An idea i had was to copy the text to an array firts them selecting the non repeated words and pushing them onto the stack.




>> Well i would like to use LIFO data structure:)

But do you need to ? You haven't mentioned any reason to require LIFO.

Do you need the words to still be in order ?
>> i would like to use

You should pick the appropriate data-structure based on the requirements. Speaking of which, can you post the actual assignment, that'll safe us a lot of posts ;)
yes i need them to still be in order and the LIFO is to train the use of data structures :)
Then you won't be able to sort the stack as Infinity suggests as it needs to have the original order...
>> yes i need them to still be in order

So, you want the words in order, without duplicates.

Then I suggest using the std::set for duplicity checks, and then add the unique words into another sequential data structure of your choice.

There still doesn't seem much reason for a stack, but oh well ...
>>There still doesn't seem much reason for a stack, but oh well ...
Totally agree with you my friend... ;)

For a stack search you could use a beam stack search algorithm, in which you can search for an item and compare to the new one you're going to insert... if it already exists then don't push it into the stack...
damn :( the  http://www.cplusplus.com/reference/stl/set/ doesnt really help me the way i want.
im starting to believe that what i want to do isnt possible.
Is possible to do a search in the stack, but as Infinity said before this is not the best approach...
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks :)