Skip to main content

Check if two strings are equal.


In this post we will see how to check if two strings are equal in C.For this we will use strcmp function. It compares two strings lexicographically i.e character by character, if the two character matches moves to next character and compares it.
It returns 0 if both strings are equal.




#include<stdio.h>
#include<string.h>

int main()
{
char str[25];
printf("Enter string 1.\n");
gets(str);
char str1[25];
printf("\nEnter string 2.\n");
gets(str1);
int i = strcmp(str, str1);
if(i==0)
{
printf("Equal.\n");
}
else
{
printf("Not Equal.\n");
}
return 0;

}

Comments

Popular posts from this blog

SNAKE AND LADDER || JAVA GAME

import java.util.*; class SnakeLadderDemo { public static void main(String args[]) { System.out.println("\t\t\t\t\t\t\tW.E.L.C.O.M.E"); System.out.println(); System.out.println("\t\t\t\t\t\t\tThis is a game of  Snake and Ladder."); System.out.println("\t\t\t\t\t\t\tInstructions."); System.out.println(); System.out.println("\t\t\t\t\t\t\tHere you will be competing against the PC."); System.out.println("\t\t\t\t\t\t\tThere are ladders and snake if you find a snake"); System.out.println("\t\t\t\t\t\t\tyour position will be reduced to the one pointed by Snake."); System.out.println("\t\t\t\t\t\t\tIf however,you meet a ladder your position will be"); System.out.println("\t\t\t\t\t\t\tincreased to the pointed square."); System.out.println("\t\t\t\t\t\t\tThe first one to reach 100 will be the Winner."); System.out.println(); System.out.println(); System.out.println("Coded By

Java program to remove all whitespaces from entered string.

import java.util.*; class RemoveWhiteSpaceFromString { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter your string"); String s=sc.nextLine(); System.out.println(); System.out.println("Removing Whitespaces"); System.out.println(); s=s.replaceAll("\\s+",""); System.out.println(s); } }