18
DecWrite a function that implements substr() function. It should accept a String and two numbers. It should return the pointer to the substring.
0 Comments | Posted by Jex kanani in Data structure
char * strsub(char*,int,int);
void main()
{
char s[20];
int pos,n;
char str1[33];
clrscr();
printf("enter the string-->");
gets(s);
printf("enter the starting position-->");
scanf("%d",&pos);
printf("enter the ending position-->");
scanf("%d",&n);
strcpy(str1,strsub(s,pos,n));
printf("%s",str1);
getch();
}
char * strsub(char *p,int pos,int n)
{
int a=0,i=0;
char *s1;
while(a!=pos)
{
p++;
a++;
}
while(i!=n)
{
*s1=*p;
p++;
i++;
s1++;
}
s1-n;
printf("%s",*s1);
return(&s1[0]);
}
void main()
{
char s[20];
int pos,n;
char str1[33];
clrscr();
printf("enter the string-->");
gets(s);
printf("enter the starting position-->");
scanf("%d",&pos);
printf("enter the ending position-->");
scanf("%d",&n);
strcpy(str1,strsub(s,pos,n));
printf("%s",str1);
getch();
}
char * strsub(char *p,int pos,int n)
{
int a=0,i=0;
char *s1;
while(a!=pos)
{
p++;
a++;
}
while(i!=n)
{
*s1=*p;
p++;
i++;
s1++;
}
s1-n;
printf("%s",*s1);
return(&s1[0]);
}
18
DecWrite a function strrindex(s,t),which returns the position of the rightmost occurance of t in s, or –1 if there is none.
0 Comments | Posted by Jex kanani in Data structure
int stridx(char*,char);
void main()
{
char s[40];char ch;int h;clrscr();
printf("\n enter vstring-->");
gets(s);
printf("enter character to find in string--->");
scanf("%c",&ch);
h=stridx(&s[0],ch);
printf("position is=%d" ,h);
getch();
}
int stridx(char *x,char ch)
{
int j,i=0;
for(j=strlen(x);j>=0;j--)
{
if(x[j]==ch)
{ i++;
return(strlen(x)-j);
} }
{return(-1);}
}
/*
void main()
{
char s[40];char ch;int h;clrscr();
printf("\n enter vstring-->");
gets(s);
printf("enter character to find in string--->");
scanf("%c",&ch);
h=stridx(&s[0],ch);
printf("position is=%d" ,h);
getch();
}
int stridx(char *x,char ch)
{
int j,i=0;
for(j=strlen(x);j>=0;j--)
{
if(x[j]==ch)
{ i++;
return(strlen(x)-j);
} }
{return(-1);}
}
/*
18
Decthat demonstrates passing structures to functions.
0 Comments | Posted by Jex kanani in Data structure
struct book
{
char name[50];
char author[80];
int callno;
};
void main()
{
struct book b1={"let us c","raman bhai vhora",9727593201};
struct book *ptr;
clrscr();
ptr=&b1;
printf("\nname=%s,\nauthor=%s,\ncallno=%d",ptr->name,ptr->author,ptr->callno);
getch();
}
{
char name[50];
char author[80];
int callno;
};
void main()
{
struct book b1={"let us c","raman bhai vhora",9727593201};
struct book *ptr;
clrscr();
ptr=&b1;
printf("\nname=%s,\nauthor=%s,\ncallno=%d",ptr->name,ptr->author,ptr->callno);
getch();
}
18
Decthat demonstrates passing arrays to functions.
0 Comments | Posted by Jex kanani in Data structure
void main()
{
int a[10],i;
int *b;
void show (int*,int);
clrscr();
for(i=0;i<=9;i++)
scanf("%d",&a[i]);
show(a,9);
getch();
}
void show(int*b,int x)
{
int i;
for(i=0;i<=x;i++)
{
printf("\n value is=%d",*(b+i));
}
getch();
}
/*
{
int a[10],i;
int *b;
void show (int*,int);
clrscr();
for(i=0;i<=9;i++)
scanf("%d",&a[i]);
show(a,9);
getch();
}
void show(int*b,int x)
{
int i;
for(i=0;i<=x;i++)
{
printf("\n value is=%d",*(b+i));
}
getch();
}
/*
18
Decwrite a prog. to compare two files, printing the first line where they differ.
0 Comments | Posted by Jex kanani in Data structure
void main()
{
FILE *fp,*fp1;
char x[50],y[50];
clrscr();
fp=fopen("d:\\j1.txt","r");
fp1=fopen("d:\\j2.txt","r");
if(fp==NULL)
{
printf("can't open");
}
if(fp1==NULL)
{
printf("can't open");
fclose(fp);
exit();
}
fgets(x,49,fp);
fgets(y,49,fp1);
{
if(strcmp(x,y)==0)
{
printf("both file are same");
}
else
{
printf("both file are not same");
printf ("\n%s",x);
printf ("\n%s",y);
}}
fclose(fp);
fclose(fp1);
getch();
}
{
FILE *fp,*fp1;
char x[50],y[50];
clrscr();
fp=fopen("d:\\j1.txt","r");
fp1=fopen("d:\\j2.txt","r");
if(fp==NULL)
{
printf("can't open");
}
if(fp1==NULL)
{
printf("can't open");
fclose(fp);
exit();
}
fgets(x,49,fp);
fgets(y,49,fp1);
{
if(strcmp(x,y)==0)
{
printf("both file are same");
}
else
{
printf("both file are not same");
printf ("\n%s",x);
printf ("\n%s",y);
}}
fclose(fp);
fclose(fp1);
getch();
}
![[del.icio.us]](http://www.itshala.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.itshala.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://www.itshala.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.itshala.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.itshala.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.itshala.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Twitter]](http://www.itshala.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.itshala.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.itshala.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.itshala.com/wp-content/plugins/bookmarkify/email.png)
