Thread: some ideas

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    some ideas

    Hey guys so I've been working on this string program for a while now and now I'm basically about to finish it. The gist of the program is a user inputs a string and then there are a number of commands he can carry out. If he types "reverse" the program displays the string in reverse. If he types "toggle" the program displays the string in toggled form etc. So the last command I want to do is when the user types "history" a history of previous typed in commands pops up. Any idea of how I should get started. Any string functions I can use?
    Any suggestions and example C programs welcome

  2. #2
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    to invert the string you can assign a char from the last position of the string minus one (because of '\0') to the first position of another array (the one you want inverted) until the end of str2. Like this:

    str1[0] = str2[8]
    str1[1] = str2[7]
    .
    .
    .
    str1[n-1] = str2[0]

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When you read the command,i guess you handle it in your program as a string and compare it with strcmp with "reverse", "toggle",etc...You do with a switch or if/else what the command says and then you read the next command from the user.I think that you shall agree on this.When you read a command,why not store it somewhere in order you to remember.Yes you agree on that too.But where should you store it?What comes in my mind is the following menu.Evaluate and select kiwi101
    • You know how many commands the user is going to give,so store an array of char pointers with the size you know and store them there.
    • Number of commands unknown(more realistic)
      • Create a beautiful and elegant simple linked list,where every node has a char pointer(dynamically allocate memory via malloc) or an array of char(set as size of this array the maximum length of the commands that you are going to handle).Do not forget that strings in C are terminated by a null terminator.Of course every node of the list must have a pointer to the next node of the list.If no next node,set the pointer to NULL.
      • Go back to the idea of array.Ask the user at the start of your program for how many commands is he going to input.Then scanf this input and store it in a variable n for example.Then this n will be the size of your array
      • Create an array of pointers to char.Set as size of it, a number that you guess will be the number of commands given.If you guess true,then ok.If not use realloc to dynamically extend the array.Well there are some reasons to all this work,but for your purposes i wouldn't recommended it.


      Functions from string.h that may be useful for you are
      • strcpy
      • strdup (instead of strcpy.I prefer strcpy)
      • strcmp
      • strlen

    General message : Whenever you allocate memory dynamically,in other words, when you allocate memory with malloc, never forget to free your memory.Do that with function free
    Hope this helps.

    EDIT -> i thought that only the history was not implemented yet :/

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You create a commands[][] array, and strcpy the commands in, after the user enters them.

    You will need to pick out reasonable sizes for the two dimensions of the commands[][] array. Maybe commands[100][20]. There are better ways of doing this. Still, I would recommend you use a single 2D char array like commands.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    210
    Okay so this is what I did tell me if I'm on the right track
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define SIZE 200
    #define HISTORY_SIZE 10 /*the maximum size of the history*/
    //#define MAX_STRING_LEN 100 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10/*the maximum size of a command*/
    
    void getString( char* );
    void printString (const char *);
    void printHistory(char history[][MAX_CMD_LEN],const char *cmd, int size);
    
    int main(void)
    {
      char command[MAX_CMD_LEN];
      char history_table[HISTORY_SIZE][MAX_CMD_LEN];
     char string[SIZE];
     int history_size = 0;
    
    do {
        printf("cmd> ");
    
    
      if(fgets(command,  MAX_CMD_LEN, stdin) != NULL){
           if((p = strchr( command, '\n')) != NULL)
             *p = '\0';
        }
    
    
        if(strcmp(command ,"new")==0) {
          getString(string);
          }
    if(strcmp(command, "list")==0) {
          printString(string);
           }
      if(strcmp(command, "hist")==0) {
                      printHistory(history_table, history_size);
                    }
     }
     while (flag==1);
    
    
    
    
    
    
           return 0;
      }
    void getString(char *string)
    {
    
    
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    }
    
    
    void printString (const char *string)
    {
    
    
    
    
    
    
      printf( "\n\nHere is the text you entered:\n%s\n", string);
    
    
    
    
    }
    void printHistory(char history[][MAX_CMD_LEN],const char *cmd, int size)
    {
      int i;
      if (size<HISTORY_SIZE){ //Put the commands in the history table.
        strcpy(history[size],cmd);
        printf("%s", history[size]);}
      //Make space if size exceeds the HISTORY_SIZE.
      if (size > HISTORY_SIZE-1)
        { for (i=0;i<9;i++)
            {
              strcpy(history[i],history[i+1]);
            }
          strcpy(history[HISTORY_SIZE-1],cmd);//Place the last element in last spot.
    
    
        }
      }

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    210
    I just posted the code to give a feel of what Im doing
    if theres an error with the code its only because I probably copy pasted wrong because it works its only the printhistory function Im worried about thanks!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,604
    It's time to learn about the value of good indentation.
    SourceForge.net: Indentation - cpwiki

    Your code is pretty horrible as presented here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    210
    Sorry letme post it again
    Last edited by kiwi101; 10-29-2012 at 02:57 PM.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    210
    Code:
    File Edit Options Buffers Tools C Help
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    
    #define SIZE 200
    #define HISTORY_SIZE 10 /*the maximum size of the history*/
    //#define MAX_STRING_LEN 100 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10/*the maximum size of a command*/
    
    
    void getString( char* );
    void printString (const char *);
    void printHistory(char history[][MAX_CMD_LEN],const char *cmd, int size);
    
    
    int main(void)
    {
      char command[MAX_CMD_LEN];
      char history_table[HISTORY_SIZE][MAX_CMD_LEN];
      char string[SIZE];
      int history_size = 0;
      char *p;
      int flag =1;
    do {
        printf("cmd> ");
    
    
    
    
        if(fgets(command,  MAX_CMD_LEN, stdin) != NULL){
          if((p = strchr( command, '\n')) != NULL)
            *p = '\0';
        }
    
    
       if(strcmp(command ,"new")==0) {
          getString(string);
        }
        if(strcmp(command, "list")==0) {
          printString(string);
        }
        if(strcmp(command, "hist")==0) {
          printHistory(history_table, history_size);
        }
     }
    
    
    while (flag==1);
    
    
    
    
    return 0;
    
    
    }
    
    
    void getString(char *string)
    {
    
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    
    }
    
    void printString (const char *string)
    {
    
    printf( "\n\nHere is the text you entered:\n%s\n", string);
    
    }
    
    void printHistory(char history[][MAX_CMD_LEN],const char *cmd, int size)
    {
    
      int i;
      if (size<HISTORY_SIZE)
    
    {
    
    //Put the commands in the history table.
            strcpy(history[size],cmd);
             printf("%s", history[size]);
    }
    
     //Make space if size exceeds the HISTORY_SIZE.
      if (size > HISTORY_SIZE-1){
          for (i=0;i<9;i++)
            
    {
              strcpy(history[i],history[i+1]);
    
     }
              strcpy(history[HISTORY_SIZE-1],cmd);//Place the last element in last spot.
     
     }
    }
    hope this is better
    Last edited by kiwi101; 10-29-2012 at 03:24 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,604
    I was thinking of something more like this
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define SIZE 200
    #define HISTORY_SIZE 10         /*the maximum size of the history */
    //#define MAX_STRING_LEN 100 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10          /*the maximum size of a command */
    
    void getString(char *);
    void printString(const char *);
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
    
    int main(void)
    {
      char command[MAX_CMD_LEN];
      char history_table[HISTORY_SIZE][MAX_CMD_LEN];
      char string[SIZE];
      int history_size = 0;
      char *p;
      int flag = 1;
    
      do {
        printf("cmd> ");
    
        if (fgets(command, MAX_CMD_LEN, stdin) != NULL) {
          if ((p = strchr(command, '\n')) != NULL)
            *p = '\0';
        }
    
        if (strcmp(command, "new") == 0) {
          getString(string);
        }
        if (strcmp(command, "list") == 0) {
          printString(string);
        }
        if (strcmp(command, "hist") == 0) {
          printHistory(history_table, history_size);
        }
      }
      while (flag == 1);
    
      return 0;
    }
    
    
    void getString(char *string)
    {
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    }
    
    void printString(const char *string)
    {
      printf("\n\nHere is the text you entered:\n%s\n", string);
    }
    
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size)
    {
      int i;
    
      if (size < HISTORY_SIZE)
      {
        //Put the commands in the history table.
        strcpy(history[size], cmd);
        printf("%s", history[size]);
      }
    
      //Make space if size exceeds the HISTORY_SIZE.
      if (size > HISTORY_SIZE - 1) {
        for (i = 0; i < 9; i++)
        {
          strcpy(history[i], history[i + 1]);
        }
        strcpy(history[HISTORY_SIZE - 1], cmd); //Place the last element in last spot.
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,604
    Why is your printHistory function printing the history, AND adding to the history?

    Perhaps you should had a separate function, say addHistory(), which you always called to add the latest command to the history, and left printHistory to print the history on demand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    210
    Okay so i seperated the 2 functions but no history is printing. Why?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define SIZE 200
    #define HISTORY_SIZE 10         /*the maximum size of the history */
    //#define MAX_STRING_LEN 100 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10          /*the maximum size of a command */
    
    
    void getString(char *);
    void printString(const char *);
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
    void addHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
    
    
    int main(void)
    {
      char command[MAX_CMD_LEN];
      char history_table[HISTORY_SIZE][MAX_CMD_LEN];
      char string[SIZE];
      int history_size = 0;
      char *p;
      int flag = 1;
    
    
      do {
        printf("cmd> ");
    
    
        if (fgets(command, MAX_CMD_LEN, stdin) != NULL) {
          if ((p = strchr(command, '\n')) != NULL)
            *p = '\0';
        }
    
    
        if (strcmp(command, "new") == 0) {
          getString(string);
        }
        if (strcmp(command, "list") == 0) {
          printString(string);
        }
        if (strcmp(command, "hist") == 0) {
          printHistory(history_table, history_size);
        }
      }
      while (flag == 1);
    
    
      return 0;
    }
    
    
    
    
    void getString(char *string)
    {
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    }
    
    
    void printString(const char *string)
    {
      printf("\n\nHere is the text you entered:\n%s\n", string);
    }
    
    
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size)
    {
      int i;
     if (size < HISTORY_SIZE)
        {
          //Put the commands in the history table.
          strcpy(history[size], cmd);
          printf("%s", history[size]);
        }
    
    
    void addHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
      //Make space if size exceeds the HISTORY_SIZE.
      {
      if (size > HISTORY_SIZE - 1) {
        for (i = 0; i < 9; i++)
          {
            strcpy(history[i], history[i + 1]);
          }
        strcpy(history[HISTORY_SIZE - 1], cmd); //Place the last element in last spot.
      }
      }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,604
    How about calling addHistory() from main at some point?

    Quote Originally Posted by myself
    Perhaps you should had a separate function, say addHistory(), which you always called to add the latest command to the history, and left printHistory to print the history on demand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    Okay so i seperated the 2 functions but no history is printing. Why?
    Your program doesn't even compile
    Code:
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
    ...
    printHistory(history_table, history_size);
    Compare the number of arguments of your declaration and of your call.

    Bye, Andreas

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    210
    new code there needs to be something at the end of add history

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define SIZE 200
    #define HISTORY_SIZE 10         /*the maximum size of the history */
    //#define MAX_STRING_LEN 100 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10          /*the maximum size of a command */
    
    
    void getString(char *);
    void printString(const char *);
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
    
    
    int main(void)
    {
      char command[MAX_CMD_LEN];
      char history_table[HISTORY_SIZE][MAX_CMD_LEN];
      char string[SIZE];
      int history_size = 0;
      char *p;
      int flag = 1;
    
    
      do {
        printf("cmd> ");
    
    
        if (fgets(command, MAX_CMD_LEN, stdin) != NULL) {
          if ((p = strchr(command, '\n')) != NULL)
            *p = '\0';
        }
    
    
        if (strcmp(command, "new") == 0) {
          getString(string);
        }
        if (strcmp(command, "list") == 0) {
          printString(string);
        }
        if (strcmp(command, "hist") == 0) {
          printHistory(history_table, command, history_size);
        }
      }
      while (flag == 1);
     return 0;
    }
    
    
    
    
    void getString(char *string)
    {
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    }
    
    
    void printString(const char *string)
    {
      printf("\n\nHere is the text you entered:\n%s\n", string);
    }
    
    
    void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size)
    {
          printf("%s", history[size]);
        }
      void addHistory(char history[][MAX_CMD_LEN], const char *cmd, int size)
    
    
      {
        int i;
    
    
        if (size < HISTORY_SIZE)
          {
            //Put the commands in the history table.
            strcpy(history[size], cmd);
    
    
      if (size > HISTORY_SIZE - 1) {
        for (i = 0; i < 9; i++)
          {
            strcpy(history[i], history[i + 1]);
          }
        strcpy(history[HISTORY_SIZE - 1], cmd); //Place the last element in last spot.
    
    
     }
    
    
          }
      }
    Last edited by kiwi101; 10-29-2012 at 05:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get this - some ideas please
    By Farhan in forum C Programming
    Replies: 3
    Last Post: 02-25-2009, 12:05 AM
  2. ANY IDEAS, would appreciate any help!
    By unejam2005 in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2005, 02:11 AM
  3. job ideas
    By dP munky in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-19-2003, 02:42 AM
  4. help with ideas
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-02-2002, 11:21 PM
  5. Any Ideas?
    By brad123 in forum C Programming
    Replies: 4
    Last Post: 04-28-2002, 09:00 AM