C Program to compare two strings

list of programs on string comparison: c program to compare two strings using strcmp function, c program to compare two strings without strcmp i.e. c program to compare two strings using arrays or  c program to compare two strings using pointers.

C Program to compare two strings using arrays without strCmp function
C Program to compare two strings using arrays without strCmp function

Accept two strings str1, str2 until end of str1 and str2 check each character in str1 is equal to character in str2. if all characters are equal or same then two strings are equal else strings are unequal.

write a C Program to compare two strings using arrays without strCmp

write a c program to compare two strings using arrays

#include<stdio.h>
void main()
{
char str1[20], str2[20];
int i;
boolean flag=0;
printf(”  c program to compare two strings without strcmp”);
printf(“enter two strings”);
scanf(“%d %d”, &str1, &str2 );
while(str1[i]!=’/0′ &&str2[i]!=’/0′)
if(str1[i]!=str2[i])
flag=1;
if(flag==1)
printf(” equal”);
else
printf(” not equal”);
}

OUTPUT:

c program to compare two strings without strcmp

enter two strings

Aksha
Varsha
not equal

write a C Program to compare two strings using arrays in a function

#include<stdio.h>
void main()
{
char string1[20], string2[20];
int i;
boolean flag=0;
printf(“c program to compare two strings without strcmp”);
printf(“enter two strings”);
scanf(“%s %s”, &string1, &string2);
Str_compare(string1, string2);
}

void Str_compare( str1[20], str2[20])
{
for( i=0; str1[i]!=’/0 && str2[i]!=’/0′; i++)
{
if(str1[i]!=str2[i])
flag=1;
}
if(flag==1)
printf(“Two strings are equal”);
else
printf(“Two strings are not equal”);
}

OUTPUT:

c program to compare two strings without strcmp
enter two strings
Aksha
Aksha
Two strings are equal

write a C Program to compare two strings using arrays and for loop

#include<stdio.h>
void main()
{
char str1[20], str2[20];
int i;
boolean flag=0;
printf(“To c program to compare two strings without strcmp”);
printf(“enter two strings”);
scanf(“%d %d”, &str1, &str2 );
for( i=0; str1[i]!=’/0 && str2[i]!=’/0′; i++)
{
if(str1[i]!=str2[i])
flag=1;
}
if(flag==1)
printf(“Two strings are equal”);
else
printf(“Two strings are not equal”);
}

OUTPUT:

c program to compare two strings without strcmp
enter two strings
Aksha
Aksha
Two strings are equal

Write a C program to compare two strings using arrays without case sensitive

Write a C program to compare two strings using arrays without case sensitive

#include<stdio.h>
void main()
{
char str1[30], str2[30];
int i;
boolean flag=0;
printf(“To Compare two strings without case sensitive”);
printf(“enter two strings”);
scanf(“%d %d”, str1, str2 );
//converting both strings to same case. Here converted to lowercase
//you can even convert both strings to uppercase
str1=lowercase(str1);
str2=lowercase(str2);
if(str1==str2)
{
printf(“Both strings are equal”);
}
getch()
}

char[] lowercase(char s[30])
{
for( i=0; s[i] !=’\0′;i++)
{
if(s[i]>64 && s[i]<92
s[i]=s[i]+32;
}
return (s[30]);
}

———————————————–

Compare two strings using pointers program logic:
Accept two strings str1, str2 until end of str1 and str2 check each character in str1 is equal to character in str2. if all characters are equal or same then two strings are equal else strings are unequal. This has to be implemented using pointers.

write a C Program to compare two strings using pointers & FOR LOOP

write a C Program to compare two strings using pointers / write a C Program for comparing two strings without strCmp

#include<STDIO.H>
#include<CONIO.H>
main()
{
char *p,*q;
int i,j,flag=0;
printf(“To compare two strings using pointers \n”);
printf(“\nInput two strings\n”);
gets(p);
gets(q);
for(;*p!=’\0′ || *q!=’\0′;*p++,*q++)
{
if(*p|=*q)
flag=1;
}
if (flag==1)
printf(“\nTwo Strings are different”);
else
printf(“\nTwo Strings are same”);
}

OUTPUT :

To compare two strings using pointers

Input two strings

Aksha

Aksha

Strings are same

write a C Program to compare two strings using pointers & while loop

write a C Program to compare strings without strCmp / write a C Program for comparing two strings using pointers

#include<STDIO.H>
#include<CONIO.H>
main()
{
char *p,*q;
int i,j,flag=0;
printf(“To compare two strings without strCmp \n”);
printf(“\nInput two strings\n”);
gets(p);
gets(q);
while(*p!=’\0′ || *q!=’\0′)
{
if(*p!=*q)
flag=1;
*p++;
*q++;
}
if (flag==0)
printf(“\nTwo Strings are same”);
else
printf(“\nTwo Strings are different”);
}

OUTPUT:

To compare two strings without strCmp
Input two strings
Hai
hello
Two Strings are different

——————————————–

c program to compare two strings using strcmp function

c program to compare two strings using strcmp function

#include<stdio.h>
#include<string.h>
int main()
{
char x[100], y[100];
printf
(“Enter the first string\n”);
gets
(x);
printf
(“Enter the second string\n”);
gets
(y);
if( strcmp(x,y) == 0 )
printf
(“Both strings are equal.\n”);
else
printf
(“Strings are not equal.\n”);
getch();
}

 

Other programs:

Static variable scope
Register variable scope
External variable scope
Automatic variable scope
amicable number
Prime number
Perfect number
Palindrome number
Strong number
Find Factorial 
Biggest Number
Average of n numbers
convert decimal number to octal
decimal number to hexadecimal
decimal number to binary
simple interest
compound interest
Add sparce matrices
Add matrices
 file copying
Palindrome string
Reverse string
String length
Replace characters in string
substring or not using arrays
First occurrence of character in string
Last occurrence of character in string
Convert to uppercase
Convert to lowercase
Concatenate strings 
Compare strings
Generate Employee payslips
Count students scoring marks>50%
convert weight into grams

 

Leave a Reply

Your email address will not be published. Required fields are marked *