Can any one help with the pointer solution to check for this -> symbol and one it finds the symbol it should move behind 4 characters and store the string
B17 in an array and then move 4 characters ahead and similarly save B21 likewise for each line do as above..
void foo( char *src, char *behind, char *infront)
{
char *p=strstr(src,"->");
*infront=*behind=0x0; // initialize to empty
if(p!=NULL)
{
char *q=p;
q-=4; // go four positions backward
memcpy(behind, q, 3);
behind[3]=0x0; // mark the end of the string
q=p;
q+=4;
memcpy(infront, q, 3);
infront[3]=0x0;
}
}
This is not succinct code but rather code to show how to do your task. Next time you should show what you have tried. Also this does zero error checking, e.g., what if you had B17-> in your file?
#include<stdio.h>
int main() {
char ch, file_name[25];
char wordChoice[2]="->";
int count=0,z=0,i=0;
FILE *fp;
char *a[100];
fp = fopen("mergesort1.txt","r");
if( fp == NULL ) {
perror("Error while opening the file.\n");
}
printf("The contents of %s file are :\n", file_name);
char buffer[2024]; //Sorry for this.
while(fgets(buffer, 2024, fp) != NULL)
{
if(z==1)
{
printf("%s",buffer);
z=0;
}
if(strstr(buffer, wordChoice))
{count++; z=z+1; }
}
fclose(fp);
printf("count:%d \n",count); }
This is the code which searches for a string ie(->)in a file which is attached below and on finding it prints the next word after the symbol.In this code how is it possible to print the previous word before the symbol and save it in an array.
The file you show is line-oriented and you read it with fgets which is line-oriented. Yet, in your task description you refer to previous word / next word, which is a bit different concept in my mind.
In any case, for you to print previous "word" (in effect, previous line) just have another
char prev_buf[2024];
variable and at the end of the loop always copy buffer into it, like
while(fgets(...))
{
... your code here ...
strcpy(prev_buf, buffer);
}
This will allow you to print the previous buffer when you encounter the key word, as in your code
if(strstr(...)) { printf("%s\n", prev_buf); up your counts etc }
I'm curious. Why are you using C? Does this program do anything besides process text? If not, and if this code is going to be used in production, given your level of proficiency, it would be wise to use a safer, higher level language which can manipulate strings without you having to directly manage memory and pointers.
Sir actually i wanna programatically find if the code submitted by the user is taking recursive approach or iterative approach.. So for this i am genarating a call graph which gives a .dot file like what i have mentioned in the post.. so this B01->B02 they act as statements, so where in the whole file i get like this dat i am storing in an array and comparing like how many times B01-> B02... appear an proceeding further..