problem with for loop while copying strings in to a two dimensional string array

This is a discussion on problem with for loop while copying strings in to a two dimensional string array within the C Programming forums, part of the General Programming Boards category; Hi people, i would be very thankful for ur help, maybe it takes a few minutes to understand the algorythm ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    problem with for loop while copying strings in to a two dimensional string array

    Hi people, i would be very thankful for ur help, maybe it takes a few minutes to understand the algorythm but if its possible take the time for it..
    its about a c code and ive wroted the question directly into the code..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define ARR_MAX 8
    
    
    
    
    int countLetters(char* string)
    {
      int counter;
      int letters = 0 ;
    
    
      for (counter = 0; string[counter] != '\0'; counter++)
      {
        letters++;
      }
      return letters;
    }
    
    
    int main()
    {
      char *text = "blablic,,.blablsub,blub,mi ma,maeh";
      int array_position = 8;
      int array[array_position];
      int length = countLetters(text);
      int text_counter;
      for(text_counter = 0, array_position = 0; text_counter < length; text_counter++)
      {
        if(text[text_counter] == ' ' || text[text_counter] == '\n' || text[text_counter] == '.' || text[text_counter] == ',')
        {
          array[array_position++] = text_counter + 1;
        }
        else
        array[array_position] = 0;
      }
    
    
      int array_counter;
      int position;
      char string_array[ARR_MAX][50] = { { 0 } };
      int counter;
      int i;
      int break_up = 1;
      for ( position = 0, counter = 0, i = 0, array_counter = 1; counter < 50 && position < ARR_MAX; counter++, i++)
      {
        if ((text[i] == ',') || (text[i] == '.') || (text[i] == '\n') || (text[i] == ' '))
        {
          if((array[array_counter] == (i+1)) && ((array[array_counter] - 1) == (array[array_counter - 1])))
          {
            array_counter++;  
            continue; //is that ok? because if yes, it should jump to the beginning of the for loop
          }
          else
          {
            string_array[position][counter] = 0;
            counter = -1;
            position++;
          }
        }
        else
        {
          string_array[position][counter] = text[i];
        }
      }
    
    
      for (i = 0; i < ARR_MAX; i++)
      {
         if (string_array[i][0] == 0) break;
         printf("%s\n", string_array[i]); //why does it print only the first one?
      }
    return 0;
    }
    i dont understand where the problem is..
    thanx in advance

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    please help me, ive to upload this programm the next 14 hours

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You won't get much help I fear, because you haven't helped us to help you:

    1) You did not describe the problem you're having

    2) You did not describe what the program is supposed to do

    3) You asked the questions inside the code itself.

    Run this little program to answer your question about "continue".

    Code:
    #include <stdio.h>
    
    int main(void) {
       int i;
       for(i=0;i<6;i++) {
          if(i==3) 
             continue;
          else
             printf("%2d ",i);
       }
       printf("\n");
       return 0;
    }
    Do you see a 3 in the output?

    In programming, you MUST deal with the specifics - yes, the devil really IS in the details - but so are the Angels (who knew?)

    If you want good help, get into those details, and be SPECIFIC about what you have now, and what is wrong or weird, or troubling, about it.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    hi, the programm is supposed to seperate a *text string into string_arrays after this four characters '.' ',' ' ' '\n'
    it does it wenn only one of this sign is between the words like "blablic,blablsub,blub,mi ma,maeh".
    and this is the code for only this situation.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ARR_MAX 8
    
    
    int countLetters(char* string)
    {
      int counter;
      int letters = 0 ;
    
      for (counter = 0; string[counter] != '\0'; counter++)
      {
        letters++;
      }
      return letters;
    }
    
    int main()
    {
      char *text = "blablic,blablsub,blub,mi ma,maeh";
      int array_position = 8;
      int array[array_position];
      int length = countLetters(text);
      int text_counter;
    
    
      int array_counter;
      int position;
      char string_array[ARR_MAX][50] = { { 0 } };
      int counter;
      int i;
      for ( position = 0, counter = 0, i = 0, array_counter = 1; counter < 50 && position < ARR_MAX; counter++, i++)
      {
        if ((text[i] == ',') || (text[i] == '.') || (text[i] == '\n') || (text[i] == ' '))
        {
            string_array[position][counter] = 0;
            counter = -1;
            position++;
    
        }
        else
        {
          string_array[position][counter] = text[i];
        }
      }
    
      for (i = 0; i < ARR_MAX; i++)
      {
         if (string_array[i][0] == 0) break;
         printf("%s\n", string_array[i]);
      }
    return 0;
    }
    and this works, but only for one only seperating sign.
    what if when ive got a *text which is (char *text = "blablic,. blablsub, blub,mi ma..,maeh"

    because of this reason ive decided to build an int array[] into the code which counts the position for the separator signs.

    like in the last text example
    Code:
    array[0] = 8;
    array[1] = 9;
    array[2] = 10;
    array[3] = 19;
    array[4] = 20;
    ....
    ...
    because of that ive written something special for it..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ARR_MAX 8
    
    
    int countLetters(char* string)
    {
      int counter;
      int letters = 0 ;
    
      for (counter = 0; string[counter] != '\0'; counter++)
      {
        letters++;
      }
      return letters;
    }
    
    int main()
    {
      char *text = "blablic,,.blablsub,blub,mi ma,maeh";
      int array_position = 8;
      int array[array_position];
      int length = countLetters(text);
      int text_counter;
      for(text_counter = 0, array_position = 0; text_counter < length; text_counter++)
      {
        if(text[text_counter] == ' ' || text[text_counter] == '\n' || text[text_counter] == '.' || text[text_counter] == ',')
        {
          array[array_position++] = text_counter + 1; //here it gets the position for separator character for the actual array[]
        }
        else
        array[array_position] = 0;
      }
    
      int array_counter;
      int position;
      char string_array[ARR_MAX][50] = { { 0 } };
      int counter;
      int i;
      for ( position = 0, counter = 0, i = 0, array_counter = 1; counter < 50 && position < ARR_MAX; counter++, i++)
      {
        if ((text[i] == ',') || (text[i] == '.') || (text[i] == '\n') || (text[i] == ' '))
        {
          if((array[array_counter] == (i+1)) && ((array[array_counter - 1]) == (array[array_counter] - 1))) //if a separator sign follows another
          {
            array_counter++;
            continue; //breakup here and continue to count at the for loop
          }
          else
          {
            string_array[position][counter] = 0; // this is for that if a new separator sign comes and last character of string array ist set to 0.
            counter = -1; 
            position++;  //this is for the next string_array
          }
        }
        else
        {
          string_array[position][counter] = text[i]; //otherwise continue to copy from text into the actual string_array
        }
      }
    
      for (i = 0; i < ARR_MAX; i++)
      {
         if (string_array[i][0] == 0) break;
         printf("%s\n", string_array[i]); //why does it print only the first one?
      }
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I print out a two dimensional array of strings?
    By nellosmomishot in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 02:47 PM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 01:20 PM
  4. Copying into an array of strings
    By mettle in forum C Programming
    Replies: 5
    Last Post: 06-14-2006, 02:18 PM
  5. copying a string to an array
    By sameintheend01 in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2003, 06:50 PM

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