Thread: Program using Stack

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    1

    Program using Stack

    Hello guys, can you help me, please.

    I need to make a program that preserving the order of the words, invert the order of the letters of a word yes and the other one of a phrase. For this use only one stack. Example:

    original string:
    TODAY THE SUN IS BEAUTIFUL

    program result:
    YADOT THE NUS IS LUFITUAEB









  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,732
    If you don't worry about punctuation, reading words is just:
    Code:
    scanf("%s", wordStr);
    For the reversing of each word, an easy implementation would be to use recursion. Or you could implement your own stack, if you want.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    456
    Quote Originally Posted by BrunoNery View Post
    Hello guys, can you help me, please.

    I need to make a program that preserving the order of the words, invert the order of the letters of a word yes and the other one of a phrase. For this use only one stack. Example:

    original string:
    TODAY THE SUN IS BEAUTIFUL

    program result:
    YADOT THE NUS IS LUFITUAEB


    So you need an input string, and room for an output string the same length of the input string. So your function signature is

    Code:
    /*
       Reverse odd-numbered words in a phrase
       Params: output - pointer to the result
                    input - the text to process
       Returns: 0 on success, -1 on fail 
    */
    int reverseoddwords(char *output, const char *input)
    We'll return -1 on fail to allow us an easy way out if fed punctuation or other data we can't process.

    Now we need a stack large enough to hold to longest word. There's a dispute about what the longest word is in English, but "anitdisestablishmentarianism" at 28 letters is a candidate. So let's make our stack 28 characters. We also need a stack top counter.

    We also need a state variable. Our states are "reversing" or "copy" and we alternate between them. Finally, we'll need an index counter to step through the string, and another to step through the output.

    So
    Code:
    #define STATE_REVERSING 1
    #define STATE_COPYING 2
    
    char stack[28];
    int stacktop = 0; /* number of items on the stack */
    int state = STATE_REVERSING; /* we start out in reversing state */
    int i; /* index input*/
    int j; /*Index output */
    Now you've got to write the actual code. It sounds like homework, so of course I won't do that for you. We iterate through the input string If we are in state reversing, we push letters onto the stack, until we hit a space. Then we pop characters off the stack, and write them to the output string, incrementing j with each write. Then we go into the copy state. In the copy state, we just copy character s from input to output, incrementing i an j in lockstep, until we hit a space, when we go to the reversing state.

    You will need to pay special attention to handling the spaces and the terminating nul correctly.

    If a word goes above 28 letters, just return -1.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    6

    Hope this will help.

    Hi here is how I approached the problem:

    -First we need to make a function that reverses a word given a pointer pointing towards the begining of that word and the length of the word.
    Code:
    void rev_chars(char *str, int n)
    {
            int i;
            char swap;
    
    //this will work reagardless of wether the length of the word is even or odd
            for (i = 0; i < n / 2; i++)
            {
                    swap = str[i];
                    str[i] = str[n - i - 1];
                    str[n - i - 1] = swap;
            }
    }

    -Then we need to make a function that searches for every words, measure their length and calls the prevoius function
    Code:
    void rev_words(char *str)
    {
            int i = 0;
            int j;
    
    
            while (str[i])
            {
    //skiping unnecessary space
                    while (str[i] == ' ')
                            i++;
    //the we measure the length of the word
                    j = 0;
                    while (str[i + j] != ' ' && str[i + j])
                            j++;
    //caling the preious funtction
                    rev_chars(str + i , j);
    //go to the next set of spaces
                    i += j;
            }
    }
    Be carefull, this function is "destructive", meaning that it will aply changes directly on the string that we give it.
    If you want this function to return a copy with the desired changes you will need to use malloc.
    But, given the problem, I guess this is kind of out of the scope of this thread.

    Anyway, I hope this answer helped.

    full code with main to test if the function works(it sould
    how knows...):
    Code:
    #include<stdio.h>
    void rev_chars(char *str, int n)
    {
            int i;
            char swap;
    
    
            for (i = 0; i < n / 2; i++)
            {
                    swap = str[i];
                    str[i] = str[n - i - 1];
                    str[n - i - 1] = swap;
            }
    }
    
    
    void rev_words(char *str)
    {
            int i = 0;
            int j;
    
    
            while (str[i])
            {
                    while (str[i] == ' ')
                            i++;
                    j = 0;
                    while (str[i + j] != ' ' && str[i + j])
                            j++;
                    rev_chars(str + i , j);
                    i += j;
            }
    }
    
    
    int main(int ac, char **av)
    {
            int i;
    for(i = 0; i < ac; i++)
    {
            rev_words(av[i]);
            printf("%s\n", av[i]);
    }
    }

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    311

    Post

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *testText = "word flip function";
    
    void flipPrint(char *text) {
        int sLength = strlen(text);
        int this = 0, last = -1;
        for (int i = 0; i <= sLength; i++ ) {
            if ( text[i] == ' ' || i == sLength ) {
                this = i;
                for (int j = this; j >= (last+1); j-- ) {
                    printf( "%c", text[j] );
                }
                last = this;
            }
        }
    }
    
    int main( int argc, char *argv[] ) {
        flipPrint(testText);
        return 0;
    }
    "without goto we would be wtf'd"

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    747
    Quote Originally Posted by Structure View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *testText = "word flip function";
    
    void flipPrint(char *text) {
        int sLength = strlen(text);
        int this = 0, last = -1;
        for (int i = 0; i <= sLength; i++ ) {
            if ( text[i] == ' ' || i == sLength ) {
                this = i;
                for (int j = this; j >= (last+1); j-- ) {
                    printf( "%c", text[j] );
                }
                last = this;
            }
        }
    }
    
    int main( int argc, char *argv[] ) {
        flipPrint(testText);
        return 0;
    }
    Modifying a string literal is undefined behavior. See STR30-C. Do not attempt to modify string literals. (If a pointer to char points to a string literal, it's a good idea to declare it as const to make it painfully obvious that it can't be modified, as in const char *testText = "word flip function";, in which case you can't pass it to the flipPrint function, and rightfully so.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About stack program
    By student111 in forum C++ Programming
    Replies: 14
    Last Post: 11-19-2013, 08:20 AM
  2. stack program
    By student111 in forum C++ Programming
    Replies: 2
    Last Post: 11-17-2013, 01:39 AM
  3. Stack program
    By trishtren in forum C Programming
    Replies: 1
    Last Post: 10-01-2009, 01:47 PM
  4. Character stack ADT program
    By alice in forum C Programming
    Replies: 1
    Last Post: 07-05-2004, 04:30 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

Tags for this Thread