#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2015
    Posts
    44
    Rep Power
    10

    Printing lines of strings


    I have a problem figuring out how to print n lines of strings (not just words).
    I managed to do that after every input line.
    But, what I want is to print the below the inputs. For example,
    n=3
    Input: 1. line: A A A
    2. line: B B B
    3. line: C C C

    Output: 1. line: A A A
    2. line: B B B
    3. line: C C C


    What I am getting is
    1. line: A A A
    A A A
    ...


    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    int length(char *str)
    {
        int l;
        for(l=0;str[l];l++);
        return l;
    }
    void read(char *str)
    {
        int i=0;
        char c;
        scanf("%s",str);
        printf("%s",str);
        do
        {
           scanf("%c",&c);
           str[i++]=c;
        }
        while(c != '\n');
        str[i-1]=0;
        printf("%s",str);
    
    }
    
    char **form(int *pn)
    {
        do
        {
            printf("n=");
            scanf("%d",pn);
        }
        while(*pn<1 || *pn>MAX);
        char **p;
        char *a;
        int i;
        p=(char **)malloc(*pn * sizeof(char *));
        a=(char *)malloc(*pn * sizeof(char));
        for(i=0;i<*pn;i++)
        {
            printf("%d. line: ",i+1);
            read(a+i);
            p[i]=a+i;
            printf("\n");
        }
        return p;
    }
    
    int main()
    {
        int pn;
        char **p;
        p=form(&pn);
        free(p);
        return 0;
    }
    Thanks for replies.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    246
    Rep Power
    34
    Try to get your printf("%s",str); out of the "read" function. You should first do all reads in a loop and then call printfs in a separate loop.

    Also I recommend you to simplify reading the line using function fgets instead picking it char by char.

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for helping here!
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,980
    Rep Power
    510
    Thoughts additional to rodiongork's fine ideas:

    You probably should learn about the strlen, strdup, and related standard library functions.

    I noticed that you used malloc twice, but used free only once. That can't be right.

    You don't test for buffer overrun. With any luck your operating system will protect you from causing WWIII. Others may not as fortunate.

    Not clear why the number of lines should be related to the line length.

    If you want to store all those strings you'll need allocate space for each of them. Frankly I think malloc is expensive so I think about large memory blocks and find ways to grab the space I'll need with just one call to malloc. Means I sometimes have to be clever with pointers. But never mind that, I think you should write a program that works with plain old arrays and avoid this dynamic memory allocation business.

    Just a quick question, if sizeof(char) is 1 byte, and in your form function i is 2, and a is 8, what is the value of a+i ?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo