Hi, I've have a problem of printing data from stack, if it's simple words,then stack works just fine,but if I try to push changed data using function(Written by a very intelligent moderator :) ), I get just the same word n times:
Intelect
Intelect
Intelect
....
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//-------------------Function--------------------------------------------
char AD(char *record, char *record_out)
{
char *p1 = record;
char *p2 = record_out;
while( *p1 )
{
// use temp pointer to locate
// end of digits
char* p3 = p1;
while( isdigit(*p3) )
p3++;
// is there a space following the digits ?
if( *p3 == ' ')
// yes, then skip the space and move on to the next char
p1 = p3+1;
else
{
// no, then was the last char put into temp buffer
// a space
if( *(p2-1) == ' ')
// yes, then overwrite that space with a new character
p2--;
p1 = p3;
}
// copy all characters up to the next digit
while(*p1 && !isdigit(*p1) )
*p2++ = *p1++;
}
}
//------------------------------------------------------------------------------
struct stack {
char *d;//data
struct stack *next;//pointer
};
typedef struct stack ELEMENT;
typedef ELEMENT *POINTER;
void push(POINTER *Top, char *a)
/* put a into the top of the stack */
{
POINTER temp;
temp = malloc(sizeof(ELEMENT));
temp->d= a;
temp->next = *Top;
*Top = temp;
}
void print_stack(POINTER Top){
POINTER copy= Top;
while(copy!=NULL)
{
printf("%s\n",copy->d);
copy=copy->next;
}
}
int main()
{
POINTER start = NULL;
char *DELIMITERS = " ";
char *str,record[100],*o;
char line[]="jjj yyyy dfsfsd sdfsdf sdfsdfsdf sdfsdfsfd sdfasdfafs Intelect";
str = strtok(line, DELIMITERS);
char recordd[sizeof(record)] = {0};
while (str)
{
AD(str, recordd); //Changing word with function(actually line,but if it works with word,then...good)
// printf("%s\n",recordd);
o = recordd;
// str=o; // But if I just try to push changed word(With function which IS needed) function
// print_stack(start) prints stack for n times ????
push(&start,str); //push the word ... and this part works!!!
str = strtok(NULL, DELIMITERS);
}
print_stack(start); //print stack..works, print changed words - don't...
system("pause");
}
So, should I try to change stack or maybe there is another/easier way?