Reputation: 69
I have a simple stack program which is trying to reverse a string. I am able to 'display' the reversed string, but when it comes to storing the character returned by the Pop() function, I am failing to do so. What I am doing below is that in the main()
after I have pushed the string to the stack, I try to use re-use str[]
to store the reversed string. Since it is an array, I should be able to modify it. However, when I do so, for some strange reason the for-loop runs only ONE time. I am guessing it is doing so because in pop()
*head = NULL
in second iteration and hence it fails.
I am failing to run the for-loop for more than once. My questions is what is preventing me from storing the reversed string?
1) Why is the for-loop running only one time when I use str[i]?
2) Am I using str[i] in some strange way which is causing this to happen?
char ret = '\0';
void Push(Node **head, char data)
{
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
return;
}
temp->next = *head;
*head = temp;
return;
}
void PrintAll(Node *temp)
{
if(temp == NULL) return;
printf("%c, ", temp->data);
PrintAll(temp->next);
}
char Pop(Node **head)
{
if(*head == NULL)
{
return '\0';
}
Node *temp = *head;
ret = temp->data;
*head = (*head)->next;
free (temp);
return ret;
}
int main()
{
Node *head = NULL;
char str[] = "helloworld";
printf("Original String is:%s\n", str);
printf("Length is:%lu\n", strlen(str));
for (int i = 0; i <=strlen(str); i++)
{
Push(&head, str[i]);
}
printf ("String has been pushed to the stack.\n");
PrintAll(head);
printf("\nString has been printed. Now popping:%ld.\n", strlen(str));
//Problem starts here in this for-loop
for (int i = 0; i <=strlen(str); i++)
{
str[i] = Pop(&head); //When these next lines are enabled, the for-loop runs only once!
printf("%c, ", str[i]); //Pop() is returning the value from a global variable 'ret'
//printf("%c ", Pop(&head)); //if two previous lines are disabled, string is displayed in reverse
}
printf("\n");
printf ("String has been popped from the stack.\n");
return 0;
}
Upvotes: 0
Views: 89
Reputation: 780974
The last thing you pushed onto the stack was the null terminator of the string. So the first thing you popped was this null terminator. When you assigned str[i] = Pop(&head);
, you made the first character the null terminator. As a result, strlen(str)
is now 0
, and the loop stops on the next iteration because 1 <= 0
is not true.
You shouldn't push the null terminator onto the stack. The first loop should use i < strlen(str)
rather than i <= strlen(str)
(a more efficient way would be str[i] != 0
).
And when popping, you shouldn't use strlen()
of the resulting string, since you don't know yet how long it will be. You should call Pop(&head)
until it returns '\0'
, which indicates that you've emptied the stack.
int main()
{
Node *head = NULL;
char str[] = "helloworld";
printf("Original String is:%s\n", str);
printf("Length is:%lu\n", strlen(str));
for (int i = 0; str[i] != 0; i++)
{
Push(&head, str[i]);
}
printf ("String has been pushed to the stack.\n");
PrintAll(head);
printf("\nString has been printed. Now popping:%ld.\n", strlen(str));
for (int i = 0; str[i] = Pop(&head); i++)
{
printf("%c, ", str[i]); //Pop() is returning the value from a global variable 'ret'
}
printf("\n");
printf ("String has been popped from the stack.\n");
return 0;
}
Upvotes: 1