strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartset
    New Member
    • Nov 2009
    • 4

    strings

    if (line[strlen(line)-1]=='\n')
    {
    line[strlen(line)-1]='\0';
    }

    If anyone expalin how this code works? why 1 is deducted ?

    thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    In C, array indexes start at 0. If strlen() returns 15 on an array that has was declared to have 14 elements, accessing array[strlen(array)] would evaluate to array[15]. Of course, as you can see this means we are going outside the bounds of the array (because the array index starts at 0). If we were to do array[strlen(array) - 1], we would then be accessing the last element of the array.

    Mark.

    P.S. Please see the posting guidelines on how to ask a question, specifically the part on using [CODE] tags.

    Comment

    • smartset
      New Member
      • Nov 2009
      • 4

      #3
      Strings

      Hello Mark,

      Thanks for replly. But still confused as I'm not getting but will happen if we pass "Hello" as a strings and if we pass "Hello World" in an array of arr[10].

      Thanks,

      Comment

      • smartset
        New Member
        • Nov 2009
        • 4

        #4
        In the following program what is the purpose of defininig these two lines code?

        if (line[strlen(line)-1]=='\n')
        {
        line[strlen(line)-1]='\0';
        }


        #include <stdio.h>
        #include <string.h>
        #include <stdlib.h>
        int main(int numParms, char *parms[])
        {
        const int LINE_SIZE = 1000;
        const int STRING_SIZE = 100;
        const int NUM_STRINGS = 100;
        FILE *infile;
        char strings[NUM_STRINGS][STRING_SIZE];
        char line[LINE_SIZE];
        char* result;
        int size;
        int count;
        size = 0;
        infile = fopen("in.txt", "r");
        result = fgets(line, LINE_SIZE, infile);
        while (result != NULL)
        {
        if (line[strlen(line)-1]=='\n')
        {
        line[strlen(line)-1]='\0';
        }
        if ((size>=NUM_STR INGS) || (strlen(line)>= STRING_SIZE))
        {
        fprintf(stderr, "\nRan out of memory.\n");
        exit(EXIT_FAILU RE);
        }
        strcpy(strings[size], line);
        printf("<%s>\n" , line);
        result = fgets(line, LINE_SIZE, infile);
        size++;
        }
        fclose(infile);
        printf("\n");
        for (count=0; count<size; count++)
        {
        printf("<%s>\n" , (char*) strings[count]);
        }
        printf("\nAll done\n");
        return 0;
        }

        Thanks.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by smartset
          Hello Mark,

          Thanks for replly. But still confused as I'm not getting but will happen if we pass "Hello" as a strings and if we pass "Hello World" in an array of arr[10].

          Thanks,
          Try it and see for yourself - experimentation is a good way to learn and understand things (though I should note this was not your original question).

          Comment

          • spiralfire
            New Member
            • Nov 2009
            • 8

            #6
            Originally posted by smartset
            Hello Mark,

            Thanks for replly. But still confused as I'm not getting but will happen if we pass "Hello" as a strings and if we pass "Hello World" in an array of arr[10].

            Thanks,
            If you create a array of 10 elements, you will have 10 positions, but the first one is 0, so you will have an array from [0] to [9], thats 10 elements.

            If you build a "Hello" string(lets say called st) it will be like this:

            Code:
            st[0] = 'H';
            st[1] = 'e';
            st[2] = 'l';
            st[3] = 'l';
            st[4] = 'o';
            st[5] = '\0';
            This array could have a minimum of 6 elements..
            strlen would return 5, so the '\0' is st[strlen(st)], so i don't think that -1 should be there.. but i can be wrong..

            the '/0' char represents the end of the string. '\n' means a change of line (enter button). So that code is reading lines of the file you specified, those end with a '\n', because you are using fgets, so that will return a line of the file, putting that string in a char array, and changing the '\n' to a end of string.

            Comment

            • smartset
              New Member
              • Nov 2009
              • 4

              #7
              Strings

              Thanks. Now I got a clear idea of this code.
              :)

              Comment

              Working...