stack peep problem

Vasconcelos
Vasconcelos used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
What do you want your peep function to do ?

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

Author

Commented:
i want it to return the position of the words repeated
Top Expert 2009

Commented:
What do you mean by words repeated ? A word that is on the stack more than once ? Any word ? Or a specific word ?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
a word that that is on the stack more than once
Top Expert 2009

Commented:
Any word ?

Can you sort the stack ? That would make things a lot easier, and definitely faster.

Author

Commented:
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?
Top Expert 2009

Commented:
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.
Top Expert 2009

Commented:
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

Author

Commented:
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';

Author

Commented:
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.
Top Expert 2009

Commented:
Could you respond to http:#25643205 and http:#25643209 please ? There's crucial information we need in order to assist you.

Author

Commented:
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
Top Expert 2009

Commented:
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.
Top Expert 2009

Commented:
>> Why did you originally choose a stack as data type ?

Or in other words, do you need the data structure to be LIFO ?

Author

Commented:
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.




Top Expert 2009

Commented:
>> 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 ;)

Author

Commented:
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...
Top Expert 2009

Commented:
>> 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...

Author

Commented:
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...
Top Expert 2009
Commented:
>> damn :( the  http://www.cplusplus.com/reference/stl/set/ doesnt really help me the way i want.

What do you mean by that ? It's a container that holds unique items, and you can easily use it to test for duplicity. If that test says that the word is already in the std::set, you don't push it on your stack. If the word is not yet in the std::set, you insert it, and push it onto the stack. You will have the information in memory twice, but the alternative is to iterate over all data each time you insert a new word (since you cannot sort the words), and that's expensive.

Author

Commented:
thanks :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial