GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Tech FAQs  >  Programming  >  C

 Print  |  
Question:  Q1. write a program to find a given number is armstrong number or not ?

Q2write a program which accepts a filename as a command line argument and reverse the contents of the file(i.e first character becomes the last character of the file and so on ?

Q3 how can i call a function given its name as a string ?

Q4 How to swap low-order byte and high order byte in an integer without using temporary variable?

Q5 If we develop a project in C, then how can we create an .exe file of it?

Q6 how to print 1 to 100 numbers without using any condition checkings ?


Answer: please let me know the answers to my email address as i having the interview in TCS so please please please please let me know


October 10, 2007 00:40:40 #5
 G Siva Prakash Reddy   Member Since: Visitor    Total Comments: N/A 

RE: Q2. Write a program which accepts a filename as a command line argument and reverse the contents of the file ( i.e., first character becomes the last character of the file and so on ) ?
 

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

static void reverse_file(char *, unsigned int);

int main(int argc, char **argv)
{
        char *ptr;
        unsigned int size, count = 0;
        FILE *fp;

        if(argc < 2)
        {
                printf("%s <file>n", argv[0]);
                return EXIT_FAILURE;
        }


        if(!(fp = fopen(argv[1], "r")))
                return EXIT_FAILURE;

        fseek( fp, 0L, SEEK_END );
        count = ftell( fp );
        rewind(fp);

        if(!(ptr = malloc(count+1)))
                return EXIT_FAILURE;

        ptr[count] = '';

        fread(ptr, count, sizeof(char), fp);

        fclose(fp);

        reverse_file(ptr, count);

return EXIT_SUCCESS;
}

static void reverse_file(char *base, unsigned int size)
{
        char *end = &base[size-1];
        if(*end == 'n')
        {
                *end = '';
                --end;
        }

        for( ; end >= base; --end)
                if(*end == 'n')
                {
                        *end = ''; 
                        printf("%sn", end+1); /* if you want read into file, using fputs u can */
                }
                printf("%sn", base);
                free(base);
}


     regards,
G. Siva Prakash Reddy

     

 

Back To Question