Thread: string operations

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    2

    string operations

    Hi,

    i want to write a function with prototype

    void makeString(char *arrayOfStrings[])

    and in it, I want it to make a string beginning with a number x, x is a global variable in my code, followed by all of arrayOfStrings, seperated by spaces.

    So eg..
    x = 5, arrayOfStrings[0] = "hello", arrayOfStrings[1] = "how", arrayOfStrings[2] = "are", arrayOfString[3] = "you?"

    and I want makeString to create:
    char * s = "5 hello how are you?"

    How would I go about doing that.. first of all I don't know how to convert an integer into a string, apart from when I do printf....

    Please help me..

    Thanks~!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Not that hard overall.... Logic is roughly as follows:

    1. Allocate enough memory to hold the entire new string.
    2. Put number and space into the string via sprintf() or snprintf().
    3. Start a loop for each element in the array of strings.
    4. For each string, call strcat() and append the string to the new string, and then add a space.


    Hence.... in your example, you'd need 21 bytes to fit "5 hello how are you?". Then you'd first put the number into the new string. Then concatenate each word onto the new string. First it would be "5 ", and then "5 hello ", etc.

    You have to take care not to print an extra space at the end, though.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's probably not going to work with this prototype. You'll need to pass all necessary data and get the result back somehow. So may-be something more like this:
    Code:
    char* makeString(int value, char *arrayOfStrings[], int sizeOfArray)
    You might need these functions: sprintf for numeric conversion and strcat for appending. You'll also need to make sure you create (or provide) a char array that is large enough to hold the concatenated string.

    First it would be "5 ", and then "5 hello ", etc.

    You have to take care not to print an extra space at the end, though.
    That problem would be solved by starting with "5", then appending: "5 ", "5 hello" etc.
    Last edited by anon; 08-13-2007 at 12:28 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    2
    This is what I've came up with so far:

    Code:
    void makeString(char *args[]) {
      int j;
      char tmpBuffer[MAX_LINE+3];
      char *tmpBufferPointer = &tmpBuffer[0];
      sprintf(tmpBufferPointer,"%d: ",numOfCommands);
      for (j = 0; j < MAX_LINE/2+1; j++) {
        if (args[j] == NULL) break;
        strcat(tmpBufferPointer, args[j]);
        strcat(tmpBufferPointer, " ");  
      }
    }
    (MAX_LINE/2 + 1 is the size of args)

    I think this will add a space right after the last non null element of args.. but I'm not sure how I can avoid that, since I don't know when args[j] will become null.

    Also, is there another way to loop through args without using a for loop? I haven't programmed in C for about 2 years and I remember there used to be some standard method to go through arrays, somehow use pointers? If anyone can jolt my memory that would be great!

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standard String operations
    By kolucoms6 in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2008, 12:47 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM