0% found this document useful (0 votes)
13 views

Ds Exp String

Ds string exp Sppu

Uploaded by

harshalmane2910
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Ds Exp String

Ds string exp Sppu

Uploaded by

harshalmane2910
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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:

Title: STRING OPERATIONS

Write a program in C to illustrate string operations without using


the library functions. Verify the operations using in-built functions.
Problem A. With pointers to arrays B. Without pointers to array
Statem 1. Substring, 2. Palindrome, 3. Compares, 4. Copy, 5. Reverse
ent

Programmer Name: Harshal Ajay Mane


Batch: G8

1. Without Pointer:

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

// Display String Function


void displayString(char str2[], int
size) { printf("Enter the length of
the string: "); scanf("%d", &size);
getchar(); // Consume newline left by
scanf printf("Enter the string: ");
for (int i = 0; i < size;
i++) { str2[i] =
getchar();
if (str2[i] == '\n') break; // Stop reading if newline is encountered
}
str2[size] = '\0'; // Null-terminate the string

printf("You entered the string: \


n"); for (int i = 0; i < size; i++)
{
printf("%c ", str2[i]);
}
DS_LAB_2024-25: Program input output 1
DS_LAB_2024-25: Program input output 2
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:

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("Enter the start position of the substring (1-based


index): "); scanf("%d", &start);
printf("Enter the end position of the
substring: "); scanf("%d", &end);

if (start < 1 || end > size || start >


end) { printf("Invalid substring
range.\n"); return;
}

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:

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;
}
str2[size] = '\0';

int isPalindrome = 1;
for (int i = 0, j = size - 1; i < j; i++, j--) {
if (str2[i] !=
str2[j]) {
isPalindrome =
0; break;
}
}

printf(isPalindrome ? "The string is a palindrome.\n" : "The string is not a


palindrome.\n");
}

// 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
}

DS_LAB_2024-25: Program input output 6


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:

str4[size1] = '\0';

printf("Enter the length of the second


string: "); scanf("%d", &size2);
getchar();
printf("Enter the second
string: "); for (int i = 0; i <
size2; i++) {
str5[i] = getchar();
if (str5[i] == '\n') break;
}
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';

for (int i = 0; i <= size;


i++) { str5[i] =
str4[i];

DS_LAB_2024-25: Program input output 7


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:

printf("String copied successfully: %s\n", str5);


}

// 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");

DS_LAB_2024-25: Program input output 9


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:

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): ");

scanf("%d", &opt);
getchar(); // Consume newline after option input

// Execute the function based on the user's


choice switch (opt) {
case 1:
displayString(str2,
size); break;
case 2:
findSubstring(str2, size, start,
end); break;
case 3:
checkPalindrome(str2,
size); break;
case 4:
compareStrings(str4, str5, size1,
size2); break;
case 5:
copyString(str4, str5,
size); break;
case 6:
reverseString(str4,
size); break;
case 7: // Exit option
printf("Exiting the program.\
n"); return 0;

DS_LAB_2024-25: Program input output 10


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:

default:
printf("Invalid option. Please select a valid option.\n");
}
}

return 0;
}

DS_LAB_2024-25: Program input output 11


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:

Output (without pointer):

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

DS_LAB_2024-25: Program input output 12


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:

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

DS_LAB_2024-25: Program input output 13


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:

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.

DS_LAB_2024-25: Program input output 14


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:

With Pointer:

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

// Display String Function


void displayString(char *str2, int
size) { printf("Enter the length of
the string: "); scanf("%d", &size);
getchar(); // Consume newline left by
scanf printf("Enter the string: ");
for (int i = 0; i < size; i++)
{
*(str2 + i) = getchar();
if (*(str2 + i) == '\n') break; // Stop reading if newline is
encountered
}
*(str2 + size) = '\0'; // Null-terminate the string

printf("You entered the string: \


n"); for (int i = 0; i < size; i++)
{
printf("%c ", *(str2 + i));
}
printf("\n");
}

// 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: ");

DS_LAB_2024-25: Program input output 16


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:

for (int i = 0; i < size; i++)


{
*(str2 + i) = getchar();
if (*(str2 + i) == '\n') break;
}
*(str2 + size) = '\0';

printf("Enter the start position of the substring (1-based


index): "); scanf("%d", &start);
printf("Enter the end position of the
substring: "); scanf("%d", &end);

if (start < 1 || end > size || start >


end) { printf("Invalid substring
range.\n"); return;
}

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;
}

DS_LAB_2024-25: Program input output 17


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:

*(str2 + size) = '\0';

int isPalindrome = 1;
for (int i = 0, j = size - 1; i < j; i++, j--) {
if (*(str2 + i) != *(str2
+ j)) { isPalindrome
= 0; break;
}
}

printf(isPalindrome ? "The string is a palindrome.\n" : "The string is not a


palindrome.\n");
}

// 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';

printf("Enter the length of the second


string: "); scanf("%d", &size2);
getchar();
printf("Enter the second
string: "); for (int i = 0; i <
size2; i++) {
*(str5 + i) = getchar();
if (*(str5 + i) == '\n') break;
DS_LAB_2024-25: Program input output 18
DS_LAB_2024-25: Program input output 19
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:

}
*(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';

for (int i = 0; i <= size; i++) {


*(str5 + i) = *(str4 + i);
}

printf("String copied successfully: %s\n", str5);


}

// Reverse String
void reverseString(char *str4, int
size) { printf("Enter the length of
the string: "); scanf("%d", &size);

DS_LAB_2024-25: Program input output 20


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:

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): ");

DS_LAB_2024-25: Program input output 21


scanf("%d", &opt);
getchar(); // Consume newline after option input

DS_LAB_2024-25: Program input output 22


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:

// Execute the function based on the user's


choice switch (opt) {
case 1:
displayString(str2,
size); break;
case 2:
findSubstring(str2, size, start,
end); break;
case 3:
checkPalindrome(str2,
size); break;
case 4:
compareStrings(str4, str5, size1,
size2); break;
case 5:
copyString(str4, str5,
size); break;
case 6:
reverseString(str4,
size); break;
case 7: // Exit option
printf("Exiting the program.\
n"); return 0;
default:
printf("Invalid option. Please select a valid option.\n");
}
}

return 0;
}

DS_LAB_2024-25: Program input output 23


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:

Output (with pointer):

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

DS_LAB_2024-25: Program input output 24


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:

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

DS_LAB_2024-25: Program input output 25


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:

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.

DS_LAB_2024-25: Program input output 26

You might also like