Making character to string?

This is a discussion on Making character to string? within the C Programming forums, part of the General Programming Boards category; Code: char grid[3][3][4] = {{{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}}, {{'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    Making character to string?

    Code:
    char grid[3][3][4] = {{{'a', 'b', 'c'},
                            {'d', 'e', 'f'},
                            {'g', 'h', 'i'}},
                          {{'j', 'k', 'l'},
                            {'m', 'n', 'o'},
                            {'p', 'q', 'r'}},
                          {{'s', 't', 'u'},
                            {'v', 'w', 'x'},
                            {'y', 'z', 'a'}}};
    If 3D array is given like that, is there a way to store a character into a string and add character into the string to make a word?

    So for example, store 'a' into the string first to make it "a", then add 'e' to the string to make "ae", then finally, store 'i' to make "aei". Is this possible by any chance?
    Last edited by jh294; 04-02-2011 at 04:24 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    What is the connection between this grid of characters and this string that you're talking about?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Quote Originally Posted by laserlight View Post
    What is the connection between this grid of characters and this string that you're talking about?
    Hm, let's say there's a string like:

    Code:
    char word[] = "aei";
    I want to make the characters into a string like I said, then check to see if it equals to the "aei" string. Hope you understand what I mean :s

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    As in you want to convert that grid of characters into a string, and say, check if word is a substring of that new string?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Quote Originally Posted by laserlight View Post
    As in you want to convert that grid of characters into a string, and say, check if word is a substring of that new string?
    Yes, convert characters into a string then check if it is equal to the word string.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Ah, but you say "equal" rather than "substring". Let's take your example: imagine that you have converted grid. What does the result look like?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Quote Originally Posted by laserlight View Post
    Ah, but you say "equal" rather than "substring". Let's take your example: imagine that you have converted grid. What does the result look like?
    Hm, I am not sure what you mean by "substring." So, what I am trying to do is, I assign the starting point and end point of the grid, so in this case would be, [0][0][0] and [0][2][2].
    so I have "a(empyty)i". Since the word I am looking for is "aei", I have to increment the grid, I think, untill it becomes "aei".

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Code:
    char array[28];
    int x = 0;
    
    for (i = 0; i < 3; i++ ) {
        for (j = 0; j < 3; j++) {
            for (k = 0; k < 3; k++) {
                array[x] = grid[i][j][k];
                x++
                }
            }
        }
    array[x] = '\0' /* null terminator */
    Now array contains the entire grid. But I'm sure that's not what you want. If it's a word-search problem then you have to consider that the word may be going in any direction, even diagonally in the grid.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Quote Originally Posted by nonoob View Post
    Code:
    char array[28];
    int x = 0;
    
    for (i = 0; i < 3; i++ ) {
        for (j = 0; j < 3; j++) {
            for (k = 0; k < 3; k++) {
                array[x] = grid[i][j][k];
                x++
                }
            }
        }
    array[x] = '\0' /* null terminator */
    Now array contains the entire grid. But I'm sure that's not what you want. If it's a word-search problem then you have to consider that the word may be going in any direction, even diagonally in the grid.
    I see that the grid now goes into array[x] one by one, but what does null terminator do? Is it for the empty space at the end of the string?

    Also, how would I do diagonal? That was actually the problem I was having.. I know how to read rows with string, but not vertical or diagonal :S,,,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  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. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 01:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21