Help with this program

This is a discussion on Help with this program within the C Programming forums, part of the General Programming Boards category; //This is the program but printing str1[j] always gives strange output. //Instead of printing the 15 characters in a sliding ...

  1. #1
    Registered User
    Join Date
    Dec 2001

    Help with this program

    //This is the program but printing str1[j] always gives strange output.
    //Instead of printing the 15 characters in a sliding pattern in the first instance it concatenates all 5 strings in the first output, 4 in the second and so on.
    My objective is to take a string and copy first 10 characters in one string, then next 10 in the next and so on.
    If there are any error in the code please correct it.
    Thanks in advance


    char str[20],str1[5][15];
    int i,j,k;


  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    First some errors
    str[20] not big enough

    the second param is the source string (str+5-i) == what???
    you say you want 10 char so why copy 15?

    Try something like this.

    int main(void)
    #define     STR_SIZE      10
    #define     ARRAY         5
        char    sInput[255],sArray[ARRAY][STR_SIZE+1];
        int     i,iLoops=0;
        //get the input
        //find the number of times to add to smaller
        //strings in array 
        //I would dynamicly(?) alloc the mem here
        //or at least check that it will fit the array
        // ie if ((iLoops >= ARRAY) == ERROR)
            //add string
            strncpy( sArray[i], sInput+(i * STR_SIZE), STR_SIZE);
    return 0;
    PS I have left a flaw in the logic because its Friday. (CRYPTIC HINT: Look at the ceiling, not the floor)
    Last edited by novacain; 12-14-2001 at 03:22 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    for(i = 0; i < 5; i++)
    for(k = 0; k < 10; k++)
    array[i][k] = string[p];
    k = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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