Skip to content

Instantly share code, notes, and snippets.

Last active February 20, 2018 18:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
FindfunctionDef finds the defination of function and returns the line number
FindFunctionDef finds function defination in a source code and returns its line number
#include <iostream>
#include <string.h>
using namespace std;
unsigned int FindFunctionDefn(char* strFunctionName, char* strSourceCode ) {
//getting position of function defination
char* pos = strstr(strSourceCode, strFunctionName);
if(pos != NULL) {
int position = pos - strSourceCode;
int count = 1;
//counting no of lines in sourceCode
for(int i = 0;i <= position - 1;i++) {
if(strSourceCode[i] == '\\' && strSourceCode[i + 1] == 'n')count++;
return count;
//if function defination is not found
else {
return 0;
int main() {
char strSourceCode[200] = "int func1(){ return 0; }\\n int func2(){ return 1; }\\n" "int main(int argc, char*argv[]){ return func2(); }\\n";
char strFunctionName[20] = "func2";
//adding function defination syntax to name of function
strcat(strFunctionName, (char *)"(){");
//displaying line no of function defination
cout<< FindFunctionDefn(strFunctionName, strSourceCode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment