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

Assignment 3

The document contains multiple C programs that perform various string operations, including comparison, concatenation, counting character occurrences, checking for palindromes, altering strings, and finding the longest string. Each program includes user input handling and utilizes standard library functions for string manipulation. The programs demonstrate fundamental concepts of string handling in C programming.

Uploaded by

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

Assignment 3

The document contains multiple C programs that perform various string operations, including comparison, concatenation, counting character occurrences, checking for palindromes, altering strings, and finding the longest string. Each program includes user input handling and utilizes standard library functions for string manipulation. The programs demonstrate fundamental concepts of string handling in C programming.

Uploaded by

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

1. Write a program to accept two strings str1 and str2. Compare them.

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>

void str_reverse(char str[]) {


int len = strlen(str);
int i, j;
char temp;

// Reverse the string


for(i = 0, j = len - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}

int main() {
char str1[100], str2[100], str3[200];
int comparison,i;

// Accept input strings


printf("Enter the first string (str1): ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character

printf("Enter the second string (str2): ");


fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character

// Compare the strings


comparison = strcmp(str1, str2);

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

2.Write a menu driven program to perform the following operations on


strings using standard library functions:
Length Copy Concatenation Compare

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

int main() {
char str1[100], str2[100], result[200];
int choice;

// Menu driven loop


do {
printf("\nMenu:\n");
printf("1. Find Length of a String\n");
printf("2. Copy One String to Another\n");
printf("3. Concatenate Two Strings\n");
printf("4. Compare Two Strings\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // To clear the newline character left by scanf

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

printf("Length of the string: %lu\n", strlen(str1));


break;
case 2:
// Copy one string to another
printf("Enter the source string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character

// Copy str1 into str2


strcpy(str2, str1);

printf("Source string copied to destination: %s\n", str2);


break;

case 3:
// Concatenate two strings
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character

// Concatenate str1 and str2 into result


strcpy(result, str1); // Copy str1 to result
strcat(result, str2); // Concatenate str2 to result

printf("Concatenated string: %s\n", result);


break;

case 4:
// Compare two strings
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character

// Compare the strings


int cmpResult = strcmp(str1, str2);
if (cmpResult == 0) {
printf("The strings are equal.\n");
} else if (cmpResult < 0) {
printf("The first string is lexicographically smaller than the second.\n");
} else {
printf("The first string is lexicographically greater than the second.\n");
}
break;

case 5:
printf("Exiting program...\n");
break;

default:
printf("Invalid choice. Please try again.\n");
}

} while(choice != 5);

return 0;
}

3. C program that counts the occurrences of a specific character in a given string:

#include <stdio.h>

int main() {
char str[100], ch;
int count = 0,i;

// Input the string


printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Input the character to be searched


printf("Enter the character to count: ");
scanf("%c", &ch);

// Loop through the string and count occurrences of the character


for ( i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++;
}
}

// Output the result


printf("The character '%c' appears %d times in the string.\n", ch, count);

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>

// Function to check if a string is a palindrome


int isPalindrome(char str[]) {
int start = 0;
int end = strlen(str) - 1;

while (start < end) {


// Skip non-alphanumeric characters if needed (you can modify this
part as per requirements)
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}

return 1; // Is a palindrome
}

int main() {
char str[100];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Remove newline character from fgets input


str[strcspn(str, "\n")] = '\0';

// Check if the string is a palindrome


if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}

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>

void alterString(char str[]) {


int i;
// Iterate through each character of the string
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') {
str[i] = '*'; // Replace space with '*'
}
else if (isdigit(str[i])) {
str[i] = '?'; // Replace digits with '?'
}
else if (isalpha(str[i])) {
if (isupper(str[i])) {
str[i] = tolower(str[i]); // Convert uppercase to lowercase
} else {
str[i] = toupper(str[i]); // Convert lowercase to uppercase
}
}
}
}

int main() {
char sentence[200];

// Accept the input sentence


printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);

// Call the function to alter the string


alterString(sentence);

// Output the altered sentence


printf("Altered sentence: %s", sentence);

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;

// Accept the number of strings


printf("Enter the number of strings: ");
scanf("%d", &n);
getchar(); // To clear the newline character left by scanf

char str[n][100]; // Array to store n strings


char longest[100]; // To store the longest string
int maxLength = 0;
// Accept n strings
for ( i = 0; i < n; i++) {
printf("Enter string %d: ", i + 1);
fgets(str[i], sizeof(str[i]), stdin);
str[i][strcspn(str[i], "\n")] = '\0'; // Remove the newline character
}

// Find the longest string


for ( i = 0; i < n; i++) {
if (strlen(str[i]) > maxLength) {
maxLength = strlen(str[i]);
strcpy(longest, str[i]); // Copy the longest string into 'longest'
}
}

// Display the longest string


printf("The longest string is: %s\n", longest);

return 0;
}

You might also like