Write the c program to display the function names in the given text file.
Rule to implement the above problem:
The input file name should be input.txt and display the output in the console.
Sample Input file (input.txt):
#include<stdio.h>
void swapping(int a, int b);
int main()
{
int m = 10, n = 20;
printf(" Values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}
void swapping(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf(" \nValues after swap m = %d\n and n = %d", a, b);
}
Sample Output:
main
swapping
Implementation of the above problem:
#include <stdio.h>
#include <string.h>
void check(char *c,int p1, int p2);
void display(char *c, int p1);
int main(int argc, char **argv)
{
FILE *fp;
char ch[100];
char *pos1, *pos2, *pos3;
fp=fopen("input.txt", "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, '('); /* check opening brace */
if (pos1)
{
pos2 = strchr(ch,')'); /* check oclosing brace */
if (pos2)
{
pos3 = strchr(ch,';'); /* check for semicolon */
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);
return 0;
}
/* To check if it is a function */
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;
}
}
}
/* To display function name */
void display(char *c,int p1)
{
int temp = p1, i;
while (c[--temp] != ' ');
for (i = temp + 1; i < p1; i++) /* Print name of function character by character */
printf("%c", c[i]);
printf("\n");
return;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: