Check if a
given String is Palindrome
#include
#include
#include
void
main()
{
char string[25], reverse_string[25] =
{'\0'};
int
i, length = 0, flag = 0;
fflush(stdin);
clrscr();
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
for (i = length - 1; i >= 0; i--)
{
reverse_string[length - i - 1] =
string[i];
}
for (i = 0; i < length; i++)
{
if (reverse_string[i] == string[i])
flag = 1;
else
flag = 0;
}
if (flag == 1)
printf("%s is a palindrome
\n", string);
else
printf("%s is not a palindrome
\n", string);
getch();
}
Output
Enter a
string
sanfoundry
sanfoundry
is not a palindrome
Enter a
string
malayalam
malayalam
is a palindrome
Read two Strings & Concatenate the Strings
#include
#include
#include
void main()
{
char string1[20], string2[20];
int i, j, pos;
clrscr();
memset(string1, 0, 20);
memset(string2, 0, 20);
printf("Enter the first string : ");
scanf("%s", string1);
printf("Enter the second string: ");
scanf("%s", string2);
printf("First string = %s\n", string1);
printf("Second string = %s\n", string2);
for (i = 0; string1[i] != '\0'; i++)
{
;
}
pos = i;
for (j = 0; string2[j] != '\0'; i++)
{
string1[i] = string2[j++];
}
string1[i] = '\0';
printf("Concatenated string = %s\n", string1);
getch();
}
Output
Enter the first string : San
Enter the second string: foundry
First string = San
Second string = foundry
Concatenated string = Sanfoundry
Replace Lowercase Characters by Uppercase & Vice-Versa
#include
#include
#include
void main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence \n");
for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
{
;
}
sentence[i] = '\0';
count = i;
printf("The given sentence is : %s", sentence);
printf("\n Case changed sentence is: ");
for (i = 0; i < count; i++)
{
ch = islower(sentence[i])? toupper(sentence[i]) :
tolower(sentence[i]);
putchar(ch);
}
getch();
}
Output
Enter a sentence
wELCOME tO sANFOUNDRY
The given sentence is : wELCOME tO sANFOUNDRY
Case changed sentence is: Welcome To Sanfoundry
Check if the Substring is present inthe given String
#include
#include
void main()
{
char str[80], search[10];
int count1 = 0, count2 = 0, i, j, flag;
clrscr();
printf("Enter a string:");
gets(str);
printf("Enter search substring:");
gets(search);
while (str[count1] != '?')
count1++;
while (search[count2] != '?')
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)
printf("SEARCH SUCCESSFUL!");
else
printf("SEARCH UNSUCCESSFUL!");
getch();
}
Output
Enter a string: hello
Enter search substring: world
SEARCH UNSUCCESSFUL!
Enter a string: helloworld
Enter search substring:ld
SEARCH SUCCESSFUL!
Accepts two Strings & Compare them
#include
#include
void main()
{
int count1 = 0, count2 = 0, flag = 0, i;
char string1[10], string2[10];
printf("Enter a string:");
gets(string1);
printf("Enter another string:");
gets(string2);
while (string1[count1] != '\0')
count1++;
while (string2[count2] != '\0')
count2++;
i = 0;
while ((i < count1) && (i < count2))
{
if (string1[i] == string2[i])
{
i++;
continue;
}
if (string1[i] < string2[i])
{
flag = -1;
break;
}
if (string1[i] > string2[i])
{
flag = 1;
break;
}
}
if (flag == 0)
printf("Both strings are equal \n");
if (flag == 1)
printf("String1 is greater than string2 \n", string1, string2);
if (flag == -1)
printf("String1 is less than string2 \n", string1, string2);
getch();
}
Output
Enter a string: hello
Enter another string: world
String1 is less than string2
Enter a string:object
Enter another string:class
String1 is greater than string2
Enter a string:object
Enter another string:object
Both strings are equal
No comments:
Post a Comment