Thread: Constantly adding characters from an array to a string

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    13

    Angry Constantly adding characters from an array to a string

    I have a 2d Array

    g[x][y]
    Each coordinate is a single character

    For each character found I need to put them in a one string

    I did something like this :
    Code:
        for (x = 0; x < LARGEUR; x++)
        {
            for(y = 0;y < HAUTEUR; y++)
            {
                if (mystere[x][y] == ESPACE)
                {
    
    
                    mottrouve[t] = g[x][y];
                    t++;
                }
            }
    
    
        }
    It works but now I need mottrouve in the solution array
    solution is declared like that :

    typedef char Mot[MAXLONGUEURMOT+1];

    solutionnerMotMystere(Grille g, Mot listeDeMotsDansLaGrille[],int nbMotsDansLaGrille, Mot solution, int *err);

    I tryed strcpy(solution, mottrouve), doesn't work

    I think It's something about the \0 character...

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Note that people tend to speak English here so a little translation would be nice, especially for such a small piece of code.

    Each coordinate is a single character
    I assume you mean each element is a char. Or are the coords, x and y, actually chars?

    typedef char Mot[MAXWORDLENGTH+1];
    It's generally considered somewhat pointless to typedef an array. It's just another thing for someone to remember when reading the code.

    I think It's something about the \0 character...
    So zero-terminate word.
    Code:
    for (x = 0; x < width; x++) {
        for (y = 0; y < length; y++) {
            if (mystery[x][y] == SPACE) {
                word[t] = g[x][y];
                t++;
            }
        }
    }
    word[t] = '\0';
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by oogabooga View Post
    Note that people tend to speak English here so a little translation would be nice, especially for such a small piece of code.


    I assume you mean each element is a char. Or are the coords, x and y, actually chars?


    It's generally considered somewhat pointless to typedef an array. It's just another thing for someone to remember when reading the code.


    So zero-terminate word.
    Code:
    for (x = 0; x < width; x++) {
        for (y = 0; y < length; y++) {
            if (mystery[x][y] == SPACE) {
                word[t] = g[x][y];
                t++;
            }
        }
    }
    word[t] = '\0';
    Thanks it worked I won't forget that last line for a while :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding colours to text and characters in C
    By mrafay in forum C Programming
    Replies: 3
    Last Post: 12-18-2010, 12:15 PM
  2. constantly print an array to the screen?
    By .exe in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 10:41 PM
  3. Adding Scores from a string array
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-11-2005, 07:27 AM
  4. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  5. Can a string be converted to an array of characters?
    By supaben34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2002, 12:18 PM

Tags for this Thread