Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added string manipulation in C #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions string manipulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <stdio.h>

int main(void) {
int ch = 1;
while (ch != 5) {
printf("MENU OPTIONS:\n1. String concatenation\n2. String comparison\n3. String length\n4. String copy\n5. Exit\n");
printf("Enter choice: ");
scanf("%d", &ch);

// Initializing strings
char str1[100];
char str2[100];

if (ch == 1) {
char str[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);

// Printing original strings
printf("Original strings: %s, %s\n", str1, str2);

// Performing operation
int i;
for (i = 0; str1[i] != '\0'; i++) {
str[i] = str1[i];
}
int index = i;
for (i = 0; str2[i] != '\0'; i++) {
str[index+i] = str2[i];
}

printf("Concatenated string: %s\n", str);
}
else if (ch == 2) {
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);

int flag = 0;

for (int i = 0; str1[i] != '\0'; i++) {
if (str1[i] != str2[i]) {
flag = 1;
break;
}
}

if (flag) {
printf("Strings are not equal\n");
}
else {
printf("Strings are equal\n");
}
}
else if (ch == 3) {
printf("Enter string: ");
scanf("%s", str1);

int count = 0;
for (int i = 0; str1[i] != '\0'; i++) {
count++;
}

printf("Length of string: %d\n", count);
}
else if (ch == 4) {
printf("Enter string: ");
scanf("%s", str1);

printf("Original string: %s(%p)\n", str1, &str1);

for (int i = 0; str1[i] != '\0'; i++) {
str2[i] = str1[i];
}

printf("Copied string: %s(%p)\n", str2, &str2);
}
else if (ch != 5) {
printf("\nInvalid option\n\n");
}
}

printf("Exit..\n");
return 0;
}