- Get link
- X
- Other Apps
Strings
Nowadays,computers are widely used for word processing application such as creating,inserting,updating and modifying textual data.besides this we need to search for particular pattern within a text,delete it,or replace it with another pattern.In C languages, a string is nothing but null-terminated character array.This means that after the last character , a null character('\o') is stored to signify the end of the character array.
When the compiler assigns a character string to character array,it automatically appends a null character to the end of the string.therefore,the size of the string should be equal to the maximum number of character in the string plus one.
Write a program to compare two strings.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[50],str2[50];
int i=0,len1=0,len1=0,s=0;
clrscr();
printf("\n Enter first string:");
gets(str1);
printf("\n Enter second string:");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1==len2)
{
while(i<len1)
{
if(str1[i]==str[i])
i++;
else break;
}
if(i==len1)
{
same=1;
printf("\n The two strings are equal");
}
}
if(len1!=len2)
printf("\n The two strings are not equal");
if(s==0)
{
if(str1[i]>str2[i])
{
printf("\n string1 is string2");
}
else if(str1[i]<str2[i])
{
printf("\n string2 is greater than string1");
}
getch();
}
}
Output
Enter the first string:Hello
Enter the second string:Hello
The two strings are equal
Nowadays,computers are widely used for word processing application such as creating,inserting,updating and modifying textual data.besides this we need to search for particular pattern within a text,delete it,or replace it with another pattern.In C languages, a string is nothing but null-terminated character array.This means that after the last character , a null character('\o') is stored to signify the end of the character array.
When the compiler assigns a character string to character array,it automatically appends a null character to the end of the string.therefore,the size of the string should be equal to the maximum number of character in the string plus one.
Write a program to compare two strings.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[50],str2[50];
int i=0,len1=0,len1=0,s=0;
clrscr();
printf("\n Enter first string:");
gets(str1);
printf("\n Enter second string:");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1==len2)
{
while(i<len1)
{
if(str1[i]==str[i])
i++;
else break;
}
if(i==len1)
{
same=1;
printf("\n The two strings are equal");
}
}
if(len1!=len2)
printf("\n The two strings are not equal");
if(s==0)
{
if(str1[i]>str2[i])
{
printf("\n string1 is string2");
}
else if(str1[i]<str2[i])
{
printf("\n string2 is greater than string1");
}
getch();
}
}
Output
Enter the first string:Hello
Enter the second string:Hello
The two strings are equal
Comments
Post a Comment