Thread: Create strings based on input int?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7

    Question Create strings based on input int?

    I am a beginner in C, and I was trying to make a program which reads an "int x" and creates a number of strings equivalent to the "x", and also name the variables accordingly in series such as:string1,string2,... . I tried many ways to do it by myself and I also looked for it in many tutorials but I couldn't figure it out. So please help me if you can Please also tell me if I haven't done my homework and where I should look because I did look for it and search for it but with no luck so far.
    Last edited by omaralqady; 03-01-2008 at 07:59 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What do you mean by 'equivalent to the "x"'?

    Have you considered arrays?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7

    Smile

    For example I was trying to create a program to calculate the GPA of a student, so first I asked about the no. of subjects he was taking "x", then I wanted the program to create a no. of strings that are named in series:sub1,sub2,sub3,... that equal the no. of subjects and then I would ask the student to enter their names one by one. Can this be done using arrays?? If so how can I make the index no. of the array element per subject a variable that is increased automatically??
    Thanks for your help
    Last edited by omaralqady; 03-02-2008 at 01:55 AM.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    If I understand correctly, you just want an array of strings:
    [sub1,sub2,sub3]
    You'd essentially have a dynamic 2-d array like this:

    int numSubjects=10; // read this in
    char **subjects;

    then you malloc it:
    subjects = (char**)malloc(sizeof(char*)*numSubjects);

    then you can assign values like an array:
    subjects[0] = "why am I taking";
    subjects[1] = "c programming?";
    subjects[2] = "calculus";

    before you quit you free it:
    free (subjects);
    Last edited by chris.r; 03-02-2008 at 01:58 AM. Reason: more specific

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yes, except the notation would be slightly different:

    sub[0],sub[1]...

    edit: beaten

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by omaralqady View Post
    I am a beginner in C, and I was trying to make a program which reads an "int x" and creates a number of strings equivalent to the "x", and also name the variables accordingly in series such as:string1,string2,... . I tried many ways to do it by myself and I also looked for it in many tutorials but I couldn't figure it out. So please help me if you can Please also tell me if I haven't done my homework and where I should look because I did look for it and search for it but with no luck so far.
    Well actually it is much simpler to avoid using arrays, by writting a progra mthat writes a program tailor made to the user needs :P.
    However my solution assumes the user has configured his path to be able to invoke the c compiler and immediately enter the desired strings. Notice the care taken to properly indent the resulting program.
    Code:
    #include <stdio.h>
    int main()
    {
            int nsub, i;
            FILE *f;
    
            printf("Enter the number of subjects:");
            scanf("%d", &nsub);
                    if( (f = fopen("prog.c","w")) == NULL)
            return printf("Error opening file.\n");
            fputs("#include <stdio.h>\n\nint main()\n{", f);
            fprintf(f, "\tint i;\n");
            for(i=0; i<nsub; i++)
                    fprintf(f, "\tchar string%d[256];\n", i);
            fprintf(f, "\n");
            for(i=0; i<nsub; i++)
            {
                    fprintf(f, "\tprintf(\"Please enter string%d:\");\n", i);
                    fprintf(f, "\tfgets(string%d, 256, stdin);\n", i);
            }
            fprintf(f, "\n\treturn 0;\n}\n");
            fclose(f);
            system("gcc -o prog prog.c");
            system("./prog");
    
            return 0;
    }
    This is just a joke actually, and not to be taken seriously.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    Cairo, Egypt
    Posts
    7

    Thumbs up

    Thank you all very much, I think I'm going to try the arrays first and see if I can do it correctly because they same easier. Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM