1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need line by line explanation of this program in detail

Discussion in 'C' started by nadanasundaram, Aug 29, 2009.

  1. nadanasundaram

    nadanasundaram New Member

    Joined:
    Aug 29, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    void string_reverse_function(char *,int);
    int main()
    {
    FILE *file_pointer;
    char buffer[100];
    char read1;
    register int index = 0;
    file_pointer = fopen("input_file","r+");
    memset(buffer,'\0',100);
    while((read1 = fgetc(file_pointer)) != EOF)
    {
    if((read1 == ' ') || (read1 == '\n'))
    {
    buffer[index] = '\0';
    string_reverse_function(buffer,index);
    memset(buffer,'\0',100);
    index = 0;
    }
    else
    {
    buffer[index] = read1;
    index++;
    }
    }
    }
    void string_reverse_function(char word[],int length)
    {
    register int index;
    char temp;
    for(index = 0;index < length/2;index++)
    {
    temp = word[index];
    word[index] = word[length -(1+index)];
    word[length - (1 + index)] = temp;
    }
    printf("%s\n",word);
    }
     
    Last edited by a moderator: Aug 29, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Why not ask the person who has written it
     
  3. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    yes sabbir u r right......
    nadanasundaram it will be better if u will ask the explanation of one or two lines..... but explainiion the total is not difficult but hactic job......
    so better state ur ur question a little better next time....
    hav a gud day...
     
  4. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    the above mentioned displays the contents of the files one by one (ie) each WORD in reverse order followed by a new line character.....

    SAMPLE i/p :
    ur file contains : I LOVE MY MOTHER INDIA

    Sample O/P :
    I
    EVOL
    YM
    REHTOM
    AIDNI

    ANALYSIS OF THE PROGRAM :

    1) since in this program , a FILE contents is gonna be made use of , a file pointer is been declared. (line 5)
    2) since you are going to store the file contents by taking it as WORD by WORD , that word is goin to be stored in a temperory Buffer array. (line 6)
    3) but in file u can read only by character by character , to first read it from the file we have a character variable read1. (line 7)
    4) for indexing that buffer array we have a index as a variable .(line 8)
    5) for opening a file . we must use the function fopen() function..... inside that function the first argument is the file name , second is the opening mode of that file.. here opening mode is "r+" , where we can both read as well as write (used for updation)..
    6) now comes a very important string function , memset(); ,
    THE SYNTAX OF THIS IS : void memset (char *s , unsigned char ch , int bytes)
    a) where 's' denotes the starting address of the array , 'ch' refers to a particular character , 'bytes' refers , Till how many bytes i have to replace the character specified by 'ch' .....
    b) then after using the function , upto the specified bytes it will be replaced by that character u have mentioned by 'ch'......
    7) now he has used , this function ..... so it fills the character '\0' upto 100 bytes of memory in the buffer array......
    8) now the program starts reading from the file , read1 gets character by character from the file pointer 'file_pointer'... and simultenously checks for END OF FILE (EOF)....
    9) so next statement is , it is checking for spaces or Newline character.... since one of the termination condition for a word in english is SPACE or A NEW LINE... so the program checks for it........
    10) A) if the above if condition becomes TRUE (ie) if the file encounters SPACE or A NEW LINE , the present buffer index is made '\0' and sent to a function called string_reverse_function() , This function will thus reverse the string which is passed as an argument.......... LIKE THIS IT MOVES............... ........... ...... ...
    B) if the Above if statement is a FALSE (ie) the file is not encountering any SPACES or A NEW LINE , character by character is copied to the buffer array
    11) Thus the program runs , and when the file encounters the EOF the while loop will terminate ................



    hope this helps u .....:charming::charming:


    thank u:):)

     

Share This Page