REVERSE A STRING
char string1[10],temp[100];
int i,count=0,j;
printf("Enter a string\n");
gets(string1);
j=strlen(string1)-1;
printf("You entered %s whose length is %d\n\n",string1,j);
while(i<j)
{
temp[i]=string1[i];
string1[i]=string1[j];
string1[j]=temp[i];
i++;
j--;
}
printf("%s",string1);
}
COMPARE TWO STRINGS
void main()
{
int compare (char[],char[]);
char str1[100],str2[100],temp[100];
int flag;
printf("Enter string1");
scanf("%s",str1);
printf("Enter string2");
scanf("%s",str2);
flag=compare(str1,str2);
if(flag==1)
printf("Both are same");
else
printf("Wrong");
}
int compare (char str1[],char str2[])
{
int i=0,j,flag=0;
while(str1[i]!='\0'&&str2[i]!='\0')
{
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if(flag==0 && str1[i]=='\0' && str2[i]=='\0')
return 1;
else return 0;
}
CHECK SUBSTRING PRESENT IN GIVEN STRING
Program 1 : Without using built-in functions:
#include <stdio.h>
int check_subsequence (char [], char[]);
void main () {
int flag;
char s1[1000], s2[1000];
printf("Input first string\n");
gets(s1);
printf("Input second string\n");
gets(s2);
flag = check_subsequence(s2, s1);
(flag)?printf("YES\n"):printf("NO\n");
getch();
}
int check_subsequence (char a[], char b[])
{
int c, d;
c = d = 0;
while (a[c] != '\0')
{
while ((a[c] != b[d]) && b[d] != '\0')
{
d++;
}
if (b[d] == '\0')
break;
d++;
c++;
}
if (a[c] == '\0')
return 1;
else
return 0;
}
Program 2: Using strcmp()
char list[15][15];
int i,flag=0,a,n;
char search[15];
printf("Enter the no. of strings");
scanf("%d",&n);
printf("Enter your the strings");
for(i=0;i<n;i++)
scanf("%s",&list[i][0]);
printf("Enter the substring to be searched");
scanf("%s",&search);
for(i=0;i<5;i++)
{
a=strcmp(&list[i][0],search);
if(a==0)
{
printf("Found");
flag=1;
break;
}
}
if(flag==0)
printf("Sorry!");
}
char string1[10],temp[100];
int i,count=0,j;
printf("Enter a string\n");
gets(string1);
j=strlen(string1)-1;
printf("You entered %s whose length is %d\n\n",string1,j);
while(i<j)
{
temp[i]=string1[i];
string1[i]=string1[j];
string1[j]=temp[i];
i++;
j--;
}
printf("%s",string1);
}
COMPARE TWO STRINGS
void main()
{
int compare (char[],char[]);
char str1[100],str2[100],temp[100];
int flag;
printf("Enter string1");
scanf("%s",str1);
printf("Enter string2");
scanf("%s",str2);
flag=compare(str1,str2);
if(flag==1)
printf("Both are same");
else
printf("Wrong");
}
int compare (char str1[],char str2[])
{
int i=0,j,flag=0;
while(str1[i]!='\0'&&str2[i]!='\0')
{
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if(flag==0 && str1[i]=='\0' && str2[i]=='\0')
return 1;
else return 0;
}
CHECK SUBSTRING PRESENT IN GIVEN STRING
Program 1 : Without using built-in functions:
#include <stdio.h>
int check_subsequence (char [], char[]);
void main () {
int flag;
char s1[1000], s2[1000];
printf("Input first string\n");
gets(s1);
printf("Input second string\n");
gets(s2);
flag = check_subsequence(s2, s1);
(flag)?printf("YES\n"):printf("NO\n");
getch();
}
int check_subsequence (char a[], char b[])
{
int c, d;
c = d = 0;
while (a[c] != '\0')
{
while ((a[c] != b[d]) && b[d] != '\0')
{
d++;
}
if (b[d] == '\0')
break;
d++;
c++;
}
if (a[c] == '\0')
return 1;
else
return 0;
}
Program 2: Using strcmp()
char list[15][15];
int i,flag=0,a,n;
char search[15];
printf("Enter the no. of strings");
scanf("%d",&n);
printf("Enter your the strings");
for(i=0;i<n;i++)
scanf("%s",&list[i][0]);
printf("Enter the substring to be searched");
scanf("%s",&search);
for(i=0;i<5;i++)
{
a=strcmp(&list[i][0],search);
if(a==0)
{
printf("Found");
flag=1;
break;
}
}
if(flag==0)
printf("Sorry!");
}
No comments:
Post a Comment