Write a C program using string functions to perform the following functionalities: Assume: The array size as 50.To concatenate two strings.To find the length of a string.To copy one string to another.To find the size of a string.To compare two strings.Write each functionality as different function and call it from the main program.Sample InputEnter String1: VelloreEnter String2: Chennai
Question
Write a C program using string functions to perform the following functionalities: Assume: The array size as 50.To concatenate two strings.To find the length of a string.To copy one string to another.To find the size of a string.To compare two strings.Write each functionality as different function and call it from the main program.Sample InputEnter String1: VelloreEnter String2: Chennai
Solution 1
Sure! Here is a step-by-step guide to writing a C program that performs the mentioned functionalities using string functions:
Step 1: Include the necessary header files:
#include <stdio.h>
#include <string.h>
Step 2: Define the maximum array size:
#define MAX_SIZE 50
Step 3: Declare the functions for each functionality:
void concatenateStrings(char str1[], char str2[]);
int findStringLength(char str[]);
void copyString(char str1[], char str2[]);
int findStringSize(char str[]);
int compareStrings(char str1[], char str2[]);
Step 4: Implement the function to concatenate two strings:
void concatenateStrings(char str1[], char str2[]) {
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
}
Step 5: Implement the function to find the length of a string:
int findStringLength(char str[]) {
int length = strlen(str);
printf("Length of the string: %d\n", length);
return length;
}
Step 6: Implement the function to copy one string to another:
void copyString(char str1[], char str2[]) {
strcpy(str1, str2);
printf("Copied string: %s\n", str1);
}
Step 7: Implement the function to find the size of a string:
int findStringSize(char str[]) {
int size = sizeof(str);
printf("Size of the string: %d\n", size);
return size;
}
Step 8: Implement the function to compare two strings:
int compareStrings(char str1[], char str2[]) {
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("String1 is less than String2\n");
} else {
printf("String1 is greater than String2\n");
}
return result;
}
Step 9: Implement the main function to call the above functions:
int main() {
char str1[MAX_SIZE], str2[MAX_SIZE];
printf("Enter String1: ");
fgets(str1, MAX_SIZE, stdin);
printf("Enter String2: ");
fgets(str2, MAX_SIZE, stdin);
// Remove the newline character from the input strings
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
concatenateStrings(str1, str2);
findStringLength(str1);
copyString(str1, str2);
findStringSize(str1);
compareStrings(str1, str2);
return 0;
}
That's it! This program will prompt the user to enter two strings, and then it will perform the mentioned functionalities using the defined functions.
Solution 2
Sure! Here is a step-by-step guide to writing a C program that performs the mentioned functionalities using string functions:
Step 1: Include the necessary header files:
#include <stdio.h>
#include <string.h>
Step 2: Define the maximum array size:
#define MAX_SIZE 50
Step 3: Declare the functions for each functionality:
void concatenateStrings(char str1[], char str2[]);
int findStringLength(char str[]);
void copyString(char str1[], char str2[]);
int findStringSize(char str[]);
int compareStrings(char str1[], char str2[]);
Step 4: Implement the function to concatenate two strings:
void concatenateStrings(char str1[], char str2[]) {
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
}
Step 5: Implement the function to find the length of a string:
int findStringLength(char str[]) {
int length = strlen(str);
printf("Length of the string: %d\n", length);
return length;
}
Step 6: Implement the function to copy one string to another:
void copyString(char str1[], char str2[]) {
strcpy(str1, str2);
printf("Copied string: %s\n", str1);
}
Step 7: Implement the function to find the size of a string:
int findStringSize(char str[]) {
int size = sizeof(str);
printf("Size of the string: %d\n", size);
return size;
}
Step 8: Implement the function to compare two strings:
int compareStrings(char str1[], char str2[]) {
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("String1 is less than String2\n");
} else {
printf("String1 is greater than String2\n");
}
return result;
}
Step 9: Implement the main function to call the above functions:
int main() {
char str1[MAX_SIZE], str2[MAX_SIZE];
printf("Enter String1: ");
fgets(str1, MAX_SIZE, stdin);
printf("Enter String2: ");
fgets(str2, MAX_SIZE, stdin);
// Remove the newline character from the input strings
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
concatenateStrings(str1, str2);
findStringLength(str1);
copyString(str1, str2);
findStringSize(str1);
compareStrings(str1, str2);
return 0;
}
That's it! This program will prompt the user to enter two strings, and then it will perform the mentioned functionalities using the defined functions.
Solution 3
Sure! Here is a step-by-step guide to writing a C program that performs the mentioned functionalities using string functions:
Step 1: Include the necessary header files:
#include <stdio.h>
#include <string.h>
Step 2: Define the maximum array size:
#define MAX_SIZE 50
Step 3: Declare the functions for each functionality:
void concatenateStrings(char str1[], char str2[]);
int findStringLength(char str[]);
void copyString(char str1[], char str2[]);
int findStringSize(char str[]);
int compareStrings(char str1[], char str2[]);
Step 4: Implement the function to concatenate two strings:
void concatenateStrings(char str1[], char str2[]) {
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
}
Step 5: Implement the function to find the length of a string:
int findStringLength(char str[]) {
int length = strlen(str);
printf("Length of the string: %d\n", length);
return length;
}
Step 6: Implement the function to copy one string to another:
void copyString(char str1[], char str2[]) {
strcpy(str1, str2);
printf("Copied string: %s\n", str1);
}
Step 7: Implement the function to find the size of a string:
int findStringSize(char str[]) {
int size = sizeof(str);
printf("Size of the string: %d\n", size);
return size;
}
Step 8: Implement the function to compare two strings:
int compareStrings(char str1[], char str2[]) {
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("String1 is less than String2\n");
} else {
printf("String1 is greater than String2\n");
}
return result;
}
Step 9: Implement the main function to call the above functions:
int main() {
char str1[MAX_SIZE], str2[MAX_SIZE];
printf("Enter String1: ");
fgets(str1, MAX_SIZE, stdin);
printf("Enter String2: ");
fgets(str2, MAX_SIZE, stdin);
// Remove the newline character from the input strings
str1[strcspn(str1, "\n")] = '\0';
str2[strcspn(str2, "\n")] = '\0';
concatenateStrings(str1, str2);
findStringLength(str1);
copyString(str1, str2);
findStringSize(str1);
compareStrings(str1, str2);
return 0;
}
That's it! This program will prompt the user to enter two strings, and then it will perform the mentioned functionalities using the defined functions.
Solution 4
Sure! Here is a step-by-step guide to writing a C program that performs the mentioned functionalities using string functions:
Step 1: Include the necessary header files:
#include <stdio.h>
#include <string.h>
Step 2: Define the maximum array size:
#define MAX_SIZE 50
Step 3: Declare the functions for each functionality:
void concatenateStrings(char str1[], char str2[]);
int findStringLength(char str[]);
void copyString(char str1[], char str2[]);
int findStringSize(char str[]);
int compareStrings(char str1[], char str2[]);
Step 4: Implement the function to concatenate two strings:
void concatenateStrings(char str1[], char str2[]) {
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
}
Step 5: Implement the function to find the length of a string:
int findStringLength(char str[]) {
int length = strlen(str);
printf("Length of the string: %d\n", length);
return length;
}
Step 6: Implement the function to copy one string to another:
void copyString(char str1[], char str2[]) {
strcpy(str1, str2);
printf("Copied string: %s\n", str1);
}
Step 7: Implement the function to find the size of a string:
int findStringSize(char str[]) {
int size = sizeof(str);
printf("Size of the string: %d\n", size);
return size;
}
Step 8: Implement the function to compare two strings:
int compareStrings(char str1[], char str2[]) {
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("String1 is less than String2\n");
} else {
printf("String1 is greater than String2\n");
}
return result;
}
Step 9: Implement the main function to call the above functions:
int main() {
char str
Similar Questions
What is the function to concatenate two strings in C programming?
A set of simple string manipulation functions are employed in _______a.<stdio.h>b.<conio.h>c.<string.h>d.<math.h>
Which of the following functions can be used to find the length of a string in C?a.length()b.strlength()c.strlen()d.size()
What is the function to compare two strings lexicographically in C programming?
What is string concatenation in C programming?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.