Skip to content

Instantly share code, notes, and snippets.

@chetanyachopra
Last active February 20, 2018 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chetanyachopra/cbaa00bf845470c785d4c0a5ae2f4225 to your computer and use it in GitHub Desktop.
Save chetanyachopra/cbaa00bf845470c785d4c0a5ae2f4225 to your computer and use it in GitHub Desktop.
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