please explain the below c program

Status
Not open for further replies.
It it is a balanced tree, then the leftmost node (left leaf) should be deleted the first, followed by its right counterpart, then the parent.. and similarly it will go on to the top till the root is freed.
 

how you saying its left node why cant it be right .we are mentioning both function for pointing left and right leaf.please correct me with the concept..
 

Just make a dummy tree structure and try to follow the recursive calls you will know how is it getting deleted.

The function definition you gave will free the memory at the last instruction. Before that it is calling itself by passing the left node pointer as argument. If the left node does exist(not NULL), then it will again call its left node until it encounters a node which doesn't have a left node.
Now I said it should be a balanced tree/ left justified binary tree, so there is no chance that a node can have a right leaf without having a left leaf. So sticking to this concept, for the last left leaf, the 2nd instruction will also be called by passing NULL (no left/ right leaf). Hence this will be the first instance when we reach the 3rd statement for actually freeing the memory.
Here it will free the memory occupied by the last (left most leaf) and return to its parent.

In its parent, as the 1st instruction is executed, it will execute the 2nd instruction and traverse to the right once. Then again start moving to left and free.

So in this manner it will come in the reverse direction and free the memory .

Sorry, if I'm bad at explaining this, but this is really tough explaining through words. If you yourself will try by drawing a tree structure and follow the command flow, it will be easy for you.
 

Code:
#include<stdio.h>

char *xstrchr(char *,char );

void main()
{
char a[50];
char b;
printf("\n enter the string:");
scanf("%s",a);
b=getchar();
char *c;
c=xstrchr(a,b);
if(c!=NULL)
{
printf("\n the character found!!!!");
}
else
printf("\n the character not found!!!");
}
char *xstrchr(char *a, char b)
{
char *c;
while(*a)
{
if(b==*a)
{
c=a;
return c;
}
a++;
}
return NULL;
}

this program is to find a character in a string and display whether it is found or not.but this program does nt take character from the user.dont know the problem

enter the string:sathish

the character not found!!!
the output i got is mentioned above...
 

Code:
#include<stdio.h>

char *xstrchr(char *,char );

void main()
{
char a[50];
char b;
printf("\n enter the string:");
b=getchar();
scanf("%s",a);

char *c;
c=xstrchr(a,b);
if(c!=NULL)
{
printf("\n the character found!!!!");
}
else
printf("\n the character not found!!!");
}
char *xstrchr(char *a, char b)
{
char *c;
while(*a)
{
if(b==*a)
{
c=a;
return c;
}
a++;
}
return NULL;
}

use this....
or else use getchar instead of scanf(); to get the string...
 
hi mathes,the code working when defined the b=getchar(),above the string input values of a ,whats the reason??
 

hi mathes,the code working when defined the b=getchar(),above the string input values of a ,whats the reason??

because when entering the first string the '\n' you entered at last will still remain in the input buffer, so the getchar will get the remaining char in the buffer...
try printing b value you will get 10 by using getchar....

look at the b's value that you dicnt entered really but it is got by from the buffer...
 
Its the problem of flushing out the STDIN buffer. STDIN is a line buffer i.e. it will flush its data only when a newline is encountered.
First when you scan the string, it will wait for user input till the user presses newline(Enter key), hence the STDIN will flush out data and that will be stored in the variable.
Now when it getchar's turn, it will take that newline and hence you cannot enter the value..
 
thanks for the explanation...
i need some more clarification in the below program.
this program checks the input string from the user and check whether each words in the entered string matches the word we entered .
Code:
#include<stdio.h>
#include<string.h>
char *xstr(char *,char *);
int main()
{
char a[50],b[50];
char *c;
printf("\n enter the string:");
fgets(a,50,stdin);
printf("\n enter the substring:");
scanf("%s",b);
c=xstr(a,b);
if(c!=NULL)
printf("\n substring present!!!!");
else
printf("\n the substring not present!!!!");
}

char *xstr(char *x,char *y)
{
char *z,*a;
char b[20][50];
a=x;
int i,j;
i=j=0;
while(*a)
{
if(*a==' ')
{
a++;
b[i++][j]='\0';
j=0;
continue;
}
b[i][j++]=*a;

a++;
}
b[i][j]='\0';
int m,cn;
cn=i;
printf("\n");
for(m=0;m<=cn;m++)
printf("%s\n",b[m]);
for(m=0;m<=cn;m++)
{
if(strcmp(b[m],y)==0)
{
z=b[m];
return z;
}
}
return NULL;
}
output i got.it has to show that substring present.but not showing .
enter the string:s

enter the substring:s

s


the substring not present!!!!
 

Code:
#include<stdio.h>
#include<string.h>
char *xstr(char *,char *);
int main()
{
char a[50],b[50];
char *c;
printf("\n enter the string:");
fgets(a,50,stdin);
printf("\n enter the substring:");
scanf("%s",b);
c=xstr(a,b);
if(c!=NULL)
printf("\n substring present!!!!");
else
printf("\n the substring not present!!!!");
}

char *xstr(char *x,char *y)
{
char *z,*a;
char b[20][50];
a=x;
int i,j;
i=j=0;
while(*a)
{
if(*a==' ' || *a =='\n')
{
a++;
b[i++][j]='\0';
j=0;
continue;
}
b[i][j++]=*a;

a++;
}
b[i][j]='\0';
int m,cn;
cn=i;
printf("\n");
//for(m=0;m<=cn;m++)
//printf("%s\n",b[m]);
for(m=0;m<=cn;m++)
{
if(strcmp(b[m],y)==0)
{
z=b[m];
return z;
}
}
return NULL;
}

Try this....
 
Last edited:

printf("\n enter the string:");
fgets(a,50,stdin);
printf("\n enter the substring:");
scanf("%s",b);

Why use fgets for 1st time and scanf for the 2nd time..?
To clear the Input buffer there are functions like fflush or __fpurge. Try those.

Code:
while(*a)
{
if(*a==' ')
{
a++;
b[i++][j]='\0';
j=0;
continue;
}
b[i][j++]=*a;

a++;
}
b[i][j]='\0';
Secondly I guess you are just separating all the words from the given sentence. But there is a problem here.
You are not giving NULL at the end of every word. Just the last word will end with a NULL, hence making the strcmp return failure always (except for the last possibly)

Also, the use of fgets for the main string will also get the '\n' into the string. Hence,
Enter the string: s
But what is in the char array (a): a[0] = 's', a[1] = '\n'

So you get the output string not found.
 
whats the solution for this??

since you are using fgets check for '\n' for the end of string.. You must consider the last word of the string as well. For last word you dont have ' ' you 'll only have '\n' at the. end. That's what the problem you feced...
 

please explain the execution in while loop
compiler-gcc ,environment -terminal in ubuntu
Code:
#include <stdio.h>
int main()
{
 char a[20],b[20];
 int i=0,j=0;
 printf("\nEnter a string:");
 fgets(a,20,stdin);  
 while(b[j++]=a[i++]);
 printf("\nCopied string is %s\n",b);
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…