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
    Posts
    1

    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
    Sucheta

    #include<stdio.h>
    #include<string.h>

    main()
    {
    char str[20],str1[5][15];
    int i,j,k;
    scanf("%s",str);
    for(i=5,j=0;i>0;i--,j++)
    strncpy(str1[j],(str+5-i),15);

    for(j=0;j<5;j++)
    printf("%s\n",str1[j]);
    }

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

    strncpy(str1[j],(str+5-i),15);
    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.

    Code:
    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 
        iLoops=lstrlen(sInput)/STR_SIZE;
        //I would dynamicly(?) alloc the mem here
        //or at least check that it will fit the array
        // ie if ((iLoops >= ARRAY) == ERROR)
        for(i=0;i<iLoops;i++)
        {
            //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 02: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
    Unregistered
    Guest
    for(i = 0; i < 5; i++)
    {
    for(k = 0; k < 10; k++)
    {
    array[i][k] = string[p];
    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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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