Ds Exp String
Ds Exp String
1. Without Pointer:
#include
<stdio.h>
#include
<string.h>
printf("\n");
}
// Substring Function
void findSubstring(char str2[], int size, int start,
int end) { printf("Enter the length of the string:
");
scanf("%d",
&size);
getchar();
printf("Enter the string:
"); for (int i = 0; i <
size; i++) {
str2[i] = getchar();
if (str2[i] == '\n') break;
}
str2[size] = '\0';
printf("Extracted substring:
"); for (int i = start - 1; i <
end; i++) {
printf("%c", str2[i]);
}
printf("\n");
}
// Palindrome Check
DS_LAB_2024-25: Program input output 3
DS_LAB_2024-25: Program input output 4
PUNE INSTITUTE OF COMPUTER
TECHNOLOGY PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:
int isPalindrome = 1;
for (int i = 0, j = size - 1; i < j; i++, j--) {
if (str2[i] !=
str2[j]) {
isPalindrome =
0; break;
}
}
// Compare Strings
void compareStrings(char str4[], char str5[], int size1,
int size2) { printf("Enter the length of the first string:
");
scanf("%d",
&size1);
getchar();
printf("Enter the first
string: "); for (int i = 0; i <
size1; i++) {
str4[i] = getchar();
if (str4[i] == '\n') break;
DS_LAB_2024-25: Program input output 5
}
str4[size1] = '\0';
if (strcmp(str4, str5) == 0) {
printf("Both strings are identical.\n");
} else {
printf("The strings are not identical.\n");
}
}
// Copy String
void copyString(char str4[], char str5[], int
size) { printf("Enter the length of the
source string: "); scanf("%d", &size);
getchar();
printf("Enter the source
string: "); for (int i = 0; i <
size; i++) {
str4[i] = getchar();
if (str4[i] == '\n') break;
}
str4[size] = '\0';
// Reverse String
void reverseString(char str4[], int
size) { printf("Enter the length of
the string: "); scanf("%d", &size);
getchar();
printf("Enter the string:
"); for (int i = 0; i <
size; i++) {
str4[i] = getchar();
if (str4[i] == '\n') break;
}
str4[size] = '\0';
printf("Reversed string:
"); for (int i = size - 1; i
>= 0; i--) {
printf("%c", str4[i]);
}
printf("\n");
}
int main()
{ int
opt;
int size = 100, size1, size2,
start, end; char str4[100],
str5[100], str2[100];
while (1) { // Keep the program running until the user selects "Exit" printf("\
nMenu:\n");
printf("1. Display String\
n"); printf("2. Substring\
DS_LAB_2024-25: Program input output 8
n");
scanf("%d", &opt);
getchar(); // Consume newline after option input
default:
printf("Invalid option. Please select a valid option.\n");
}
}
return 0;
}
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 1
Enter the length of the
string: 10 Enter the string:
SHERU
You entered the string:
S H E R U
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 2
Enter the length of the
string: 5 Enter the string:
CARGO
Enter the start position of the substring (1-based
index): 1 Enter the end position of the substring: 3
Extracted substring: CAR
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 3
Enter the length of the
string: 5 Enter the string:
MADAM
The string is a palindrome.
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 4
Enter the length of the first
string: 4 Enter the first string:
SHER
Enter the length of the second
string: 8 Enter the second string:
MUJAFFAR The strings are not
identical.
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 5
Enter the length of the source
string: 6 Enter the source string:
IRSHAD String copied
successfully: IRSHAD
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 6
Enter the length of the
string: 4 Enter the string:
SONU Reversed string:
UNOS
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-
7): 7 Exiting the
program.
With Pointer:
#include
<stdio.h>
#include
<string.h>
// Substring Function
void findSubstring(char *str2, int size, int start,
int end) { printf("Enter the length of the string:
");
scanf("%d",
&size);
DS_LAB_2024-25: Program input output 15
getchar();
printf("Enter the string: ");
printf("Extracted substring:
"); for (int i = start - 1; i <
end; i++) {
printf("%c", *(str2 + i));
}
printf("\n");
}
// Palindrome Check
void checkPalindrome(char *str2, int
size) { printf("Enter the length of
the string: "); scanf("%d", &size);
getchar();
printf("Enter the string:
"); for (int i = 0; i <
size; i++) {
*(str2 + i) = getchar();
if (*(str2 + i) == '\n') break;
}
int isPalindrome = 1;
for (int i = 0, j = size - 1; i < j; i++, j--) {
if (*(str2 + i) != *(str2
+ j)) { isPalindrome
= 0; break;
}
}
// Compare Strings
void compareStrings(char *str4, char *str5, int size1,
int size2) { printf("Enter the length of the first string:
");
scanf("%d",
&size1);
getchar();
printf("Enter the first
string: "); for (int i = 0; i <
size1; i++) {
*(str4 + i) = getchar();
if (*(str4 + i) == '\n') break;
}
*(str4 + size1) = '\0';
}
*(str5 + size2) = '\0';
if (strcmp(str4, str5) == 0) {
printf("Both strings are identical.\n");
} else {
printf("The strings are not identical.\n");
}
}
// Copy String
void copyString(char *str4, char *str5, int
size) { printf("Enter the length of the
source string: "); scanf("%d", &size);
getchar();
printf("Enter the source
string: "); for (int i = 0; i <
size; i++) {
*(str4 + i) = getchar();
if (*(str4 + i) == '\n') break;
}
*(str4 + size) = '\0';
// Reverse String
void reverseString(char *str4, int
size) { printf("Enter the length of
the string: "); scanf("%d", &size);
getchar();
printf("Enter the string:
"); for (int i = 0; i <
size; i++) {
*(str4 + i) = getchar();
if (*(str4 + i) == '\n') break;
}
*(str4 + size) = '\0';
printf("Reversed string:
"); for (int i = size - 1; i
>= 0; i--) {
printf("%c", *(str4 + i));
}
printf("\n");
}
int main()
{ int
opt;
int size = 100, size1, size2,
start, end; char str4[100],
str5[100], str2[100];
while (1) { // Keep the program running until the user selects "Exit" printf("\
nMenu:\n");
printf("1. Display String\n");
printf("2. Substring\n");
printf("3. Check Palindrome\
n"); printf("4. Compare
Strings\n"); printf("5. Copy
String\n"); printf("6. Reverse
String\n");
printf("7. Exit\n"); // Added Exit
option printf("Choose an option (1-
7): ");
return 0;
}
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 1
Enter the length of the
string: 6 Enter the string:
RIZWAN You entered the
string:
RIZWAN
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 2
Enter the length of the
string: 5 Enter the string:
ABRAR
Enter the start position of the substring (1-based
index): 1 Enter the end position of the substring: 3
Extracted substring: ABR
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 3
Enter the length of the
string: 3 Enter the string:
MOM
The string is a palindrome.
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 4
Enter the length of the first
string: 5 Enter the first string:
ABRAR
Enter the length of the second
string: 6 Enter the second string:
RIZWAN
The strings are not identical.
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 5
Enter the length of the source
string: 5 Enter the source string:
ARHAM String copied
successfully: ARHAM
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-7): 6
Enter the length of the
string: 4 Enter the string:
RAJU Reversed string: UJAR
Menu:
1.Display String
2.Substring
3.Check Palindrome
4.Compare Strings
5.Copy String
6.Reverse String
7.Exit
Choose an option (1-
7): 7 Exiting the
program.