Assignment 3
Assignment 3
If they are equal, display their length. If str1 < str2, concatenate
str1 and the reversed str2 into str3. If str1 > str2, convert str1 to
uppercase and str2 to lowercase and display. Refer sample code for
string functions above.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str1[100], str2[100], str3[200];
int comparison,i;
if (comparison == 0) {
// If str1 and str2 are equal, display their length
printf("The strings are equal.\n");
printf("Length of str1 and str2: %d\n", strlen(str1));
} else if (comparison < 0) {
// If str1 is lexicographically less than str2, concatenate str1 and
the reversed str2 into str3
str_reverse(str2); // Reverse str2
strcpy(str3, str1); // Copy str1 into str3
strcat(str3, str2); // Concatenate reversed str2 into str3
printf("str1 < str2, the concatenated string is: %s\n", str3);
} else {
// If str1 is lexicographically greater than str2, convert str1 to
uppercase and str2 to lowercase
for ( i = 0; str1[i]; i++) {
str1[i] = toupper(str1[i]);
}
for (i = 0; str2[i]; i++) {
str2[i] = tolower(str2[i]);
}
printf("str1 > str2, str1 in uppercase: %s\n", str1);
printf("str2 in lowercase: %s\n", str2);
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], result[200];
int choice;
switch(choice) {
case 1:
// Length of a string
printf("Enter a string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character
case 3:
// Concatenate two strings
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character
case 4:
// Compare two strings
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while(choice != 5);
return 0;
}
#include <stdio.h>
int main() {
char str[100], ch;
int count = 0,i;
return 0;
}
4.A palindrome is a string that reads the same-forward and reverse. Example:
“madam” is a Palindrome. Write a function which accepts a string and
returns 1 if the string is a palindrome and 0 otherwise. Use this function in
main.
#include <stdio.h>
#include <string.h>
return 1; // Is a palindrome
}
int main() {
char str[100];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
return 0;
}
5.Write a program which accepts a sentence from the user and
alters it as follows: Every space is replaced by *, case of all
alphabets is reversed, digits are replaced by ?
#include <stdio.h>
#include <ctype.h>
int main() {
char sentence[200];
return 0;
}
6.Write a program that accepts n strings and displays the longest string.
#include <stdio.h>
#include <string.h>
int main() {
int n,i;
return 0;
}