program to make an substrng
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
void main()
{
int i,j=0,l=0,p[50],pos,n;
char str[50],substr[50],dir[6],dir1[4]="left",k;
clrscr();
printf("nnEnter a string:");
scanf("%s",str);
printf("nEnter position, no. of character direction of sub string:");
scanf("%d%d%s",&pos,&n,dir);
for(i=0;str[i]!='';i++)
l++;
for(i=0;dir[i]!='';i++)
{
if(toupper(dir[i])==toupper(dir1[i]))
k='l';
else
{
k='r';
break;
}
}
if(toupper(k)=='L')
{
if(pos==0||(pos-n)<0)
{
textcolor(RED);
cprintf("nnSub string not possible");
cprintf("nPress any key to exit .....n");
getch();
exit(0);
}
for(i=pos-1;i>pos-n-1;i--)
{
substr[j]=str[i];
j++;
}
substr[j]='';
printf("nnnSubstring of %d chars from position %d towards letf in %s is ",n,pos,str);
textcolor(CYAN);
cprintf("%s.",substr);
}
else if(toupper(k)=='R')
{
if(pos>l||(pos+n-1)>l)
{
textcolor(RED);
cprintf("nnSub string not possible");
cprintf("nPress any key to exit .....n");
getch();
exit(0);
}
for(i=pos-1;i<pos+n-1;i++)
{
substr[j]=str[i];
j++;
}
substr[j]='';
printf("nnnSubstring of %d chars from position %d towards right in %s is ",n,pos,str);
textcolor(CYAN);
cprintf("%s",substr);
}
else
{
textcolor(RED);
cprintf("nnSubstring not possible....");
cprintf("nPress any key to exit....");
getch();
exit(0);
}
getch();
}
