Tuesday, October 28, 2014

C Practical program



Find Sum of Numbers given in Command Line Arguments Recursively

#include
#include
int count, s = 0;
void sum(int *, int *);

void main(int argc, char *argv[])
{
    int i, ar[argc];
    count = argc;
    for (i = 1; i < argc; i++)
    {
        ar[i - 1] = atoi(argv[i]);
    }
    sum(ar, ar + 1);
    printf("%d", s);
}

void sum(int  *a, int  * b)
{
    if (count == 1)
        return;
    s = s + *a + *b;
    count -= 2;
    sum(a + 2, b + 2);
}

Output

        Sum is 10

Display the Function Names defined in C Source File

#include 
#include 
#include 
 
void check(char *c,int p1, int p2);
void display(char *c, int p1);
 
void main(int argc, char **argv)
{
    FILE *fp;
    char ch[100];
    char *pos1, *pos2, *pos3;
 
    fp=fopen(argv[1], "r");
    if (fp == NULL)
    {
        printf("\nFile unable to open");
        return;
    }
    else
        printf("\nFile Opened to display function names :\n");
    while (1)
    {
        if ((fgets(ch, 100, fp)) != NULL)
        {
            if ((strstr(ch, "/*")) == NULL)
            {
                pos1 = strchr(ch, '(');                
                if (pos1)
                {
                    pos2 = strchr(ch,')');            
                    if (pos2)
                    {
                        pos3 = strchr(ch,';');        
                        if ((pos1 < pos2) && (pos3 == NULL) || (pos3 < pos1))
                        {
                            check(ch, pos1 - ch, pos2 - ch);
                        }
                        else    continue;
                    }
                    else    continue;
                }
                else    continue;
            }
            else    continue;
        }
        else    break;
    }
    fclose(fp);
}
 void check(char *c, int p1, int p2)
{
    int i, flag = 0, temp = p1;
 
    if ((c[p1 + 1] == ')'))
    {
        display(c, p1);
        return;
    }
    for (i = p1 + 1; i < p2; i++)
    {
        if ((c[i] != ' ') || (c[i] == ')'))
        {
            flag = 1;
 
        }
        if (flag == 0)
        {
            display(c, p1);
            return;
        }
        else
        {
            flag = 0;
            while (c[--temp] != ' ');
            for (i = 0; i < temp; i++)
                if (c[i]==' ')
                {
                    flag = 1;
                }
                if (flag == 0)
                {
                    display(c, p1);
                    return;
                 }
                 else
                     return;
          }
    }
}
 
void display(char *c,int p1)
{
    int temp = p1, i;
 
    while (c[--temp] != ' ');
    for (i = temp + 1; i < p1; i++)            
             printf("%c", c[i]);
    printf("\n");
    return;
}

Output
 
File Opened to display function names :
main
check
display

Find the Size of File using File Handling Function

#include 
#include 
void main(int argc, char **argv)
{
    FILE *fp;
    char ch;
    int size = 0;
 
    fp = fopen(argv[1], "r");
    if (fp == NULL)
        printf("\nFile unable to open ");
    else 
        printf("\nFile opened ");
    fseek(fp, 0, 2);    
    size = ftell(fp);   
    printf("The size of given file is : %d\n", size);    
    fclose(fp);
}

Output
 
File opened The size of given file is : 3791744

Capitalize First Letter of every Word in a File

#include 
#include 
#include 
int to_initcap_file(FILE *); 
 
void main(int argc, char * argv[])
{
    FILE *fp1;
    char fp[10];
    int p;
 
    fp1 = fopen(argv[1], "r+");
    if (fp1 == NULL)
    {
        printf("cannot open the file ");
        exit(0);
    }
    p = to_initcap_file(fp1);
    if (p == 1)    
    {    
        printf("success");
    }
    else
    {
        printf("failure");
    }
    fclose(fp1);
}
 
int to_initcap_file(FILE *fp)
{
    char c;
 
    c = fgetc(fp);
    if (c >= 'a' && c <= 'z')
    {
        fseek(fp, -1L, 1);
        fputc(c - 32, fp);
    }
    while(c != EOF)    
    {
        if (c == ' ' || c == '\n')
        {
            c = fgetc(fp);
            if (c >= 'a' && c <= 'z')
            {
                fseek(fp, -1L, 1);
                fputc(c - 32, fp);
            }
        }
        else
        {
            c = fgetc(fp);
        }
    }
    return 1;
}

Output
 
success
$ cat sample
Wipro Technologies
File Copy Function
Successfully Read


No comments:

Post a Comment