Write the functions to perform the following : 10m Dec2005

By | June 11, 2014

Write the functions to perform the following : 10m Dec2005

 To accept a string and print the rightmost “n” characters

  #include<stdio.h>
#include<string.h>
#include<alloc.h>
char *rightsub(char *,int n);
void main()
{
char s1[20],s2[20],ch,*s;
int n;
clrscr();
printf(“enter the string s1:”);
gets(s1);
printf(“enter the no of characters to extract=”);
scanf(“%d”,&n);
s=rightsub(s1,n);
printf(“\nright sub string: %s”,s);
free(s);
getch();
}
char *rightsub(char *str,int n)
    {
    char *t=(char *)malloc(n+1);
    int l=strlen(str);
    char *s=str+(l-n);
    int i=0;
    while(i<n)
 {
 t[i]=*s;
 s++;
 i++;
 }
    t[i]=’\0′;
    return t;
    }

[codesyntax lang=”c”]

  #include<stdio.h>
#include<string.h>
#include<alloc.h>
char *rightsub(char *,int n);
void main()
{
char s1[20],s2[20],ch,*s;
int n;
clrscr();
printf("enter the string s1:");
gets(s1);
printf("enter the no of characters to extract=");
scanf("%d",&n);
s=rightsub(s1,n);
printf("\nright sub string: %s",s);
free(s);
getch();
}
char *rightsub(char *str,int n)
    {
    char *t=(char *)malloc(n+1);
    int l=strlen(str);
    char *s=str+(l-n);
    int i=0;
    while(i<n)
 {
 t[i]=*s;
 s++;
 i++;
 }
    t[i]='\0';
    return t;
    }

[/codesyntax]

To accept any two strings and check whether the first string is a substring of the second string

#include<stdio.h>
#include<conio.h>

void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;

clrscr();

puts(“Enter a string:”);
gets(str);

puts(“Enter search substring:”);
gets(search);

while (str[count1]!=’\0′)
count1++;

while (search[count2]!=’\0′)
count2++;

for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts(“SEARCH SUCCESSFUL!”);
else
puts(“SEARCH UNSUCCESSFUL!”);
getch();
}

[codesyntax lang=”c”]

#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
clrscr();
puts("Enter a string:");
gets(str);
puts("Enter search substring:");
gets(search);
while (str[count1]!='\0')
count1++;
while (search[count2]!='\0')
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}

[/codesyntax]

Leave a Reply