Skip to content

Commit f385894

Browse files
authored
String Programs
1 parent 92d102b commit f385894

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

Compare_Strings.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
5+
void main()
6+
{
7+
char string1[20], string2[20];
8+
int compare;
9+
10+
printf("\nEnter the First String :: ");
11+
gets_s(string1, 20);
12+
13+
printf("\nEnter the Second String :: ");
14+
gets_s(string2, 20);
15+
16+
compare=strcmp(string1,string2);
17+
18+
if (compare == 0)
19+
printf("\nBoth the Strings are Equal ");
20+
else if (compare > 0)
21+
printf("\n1st String '%s' is greater than the 2nd String '%s' \ni.e. 2nd String is Alphabetically First", string1, string2);
22+
else
23+
printf("\n2nd String '%s' is greater than the 1st String '%s' \ni.e. 1st String is Alphabetically First", string2, string1);
24+
_getch();
25+
26+
}

Input_Output.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void main()
5+
{
6+
char name[20], h[20] = "Have a Nice Day";
7+
8+
printf("Enter Your Name :: ");
9+
//scanf_s("%s", name, 20);
10+
gets_s(name, 20);
11+
12+
printf("\nHello :: %s", name);
13+
printf("\n%s", h);
14+
_getch();
15+
16+
}

Palindrome_with_Function.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
5+
void main()
6+
{
7+
char original[20], reverse[20];
8+
int compare;
9+
10+
printf("\nEnter the String :: ");
11+
gets_s(original, 20);
12+
13+
strcpy_s(reverse, original); /*Makes a copy of the Original String*/
14+
_strrev(reverse); /*Reverses the String*/
15+
16+
compare = strcmp(original, reverse);
17+
18+
if (compare == 0)
19+
printf("\nThe String '%s' is a Palindrome",original);
20+
else
21+
printf("\nThe String '%s' is not a Palindrome", original);
22+
_getch();
23+
24+
}

Palindrome_without_Function.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<string.h>
4+
5+
void main()
6+
{
7+
char original[20], reverse[20];
8+
int compare, i = 0, j = 0, count = 0, check = 1;
9+
10+
printf("\nEnter the String :: ");
11+
gets_s(original, 20);
12+
13+
//printf("%d", strlen(original));
14+
while (original[i] != '\0') /*Counts the Number of Characters in the String*/
15+
{
16+
i++;
17+
count++;
18+
}
19+
20+
for (i = 0, j = count - 1; i < count; i++, j--) /*Reverses the String*/
21+
reverse[j] = original[i];
22+
reverse[count] = '\0'; /*Adding the End of String Character*/
23+
24+
for(i=0;i<count;i++)
25+
if (reverse[i] != original[i])
26+
{
27+
check = 0;
28+
break;
29+
}
30+
31+
if (check)
32+
printf("\nThe String '%s' is a Palindrome", original);
33+
else
34+
printf("\nThe String '%s' is not a Palindrome", original);
35+
_getch();
36+
37+
}

0 commit comments

Comments
 (0)