Knowee
Questions
Features
Study Tools

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.

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.

🧐 Not the exact question you are looking for?Go ask a question

Solution

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: Declare the necessary variables.

#define SIZE 50

char str1[SIZE], str2[SIZE], str3[SIZE];
int length;

Step 3: Concatenate two strings.

printf("Enter the first string: ");
fgets(str1, SIZE, stdin);

printf("Enter the second string: ");
fgets(str2, SIZE, stdin);

strcat(str1, str2);
printf("Concatenated string: %s\n", str1);

Step 4: Find the length of a string.

printf("Enter a string: ");
fgets(str1, SIZE, stdin);

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

Step 5: Copy one string to another.

printf("Enter a string: ");
fgets(str1, SIZE, stdin);

strcpy(str2, str1);
printf("Copied string: %s\n", str2);

Step 6: Find the size of a string.

printf("Enter a string: ");
fgets(str1, SIZE, stdin);

length = sizeof(str1);
printf("Size of the string: %d\n", length);

Step 7: Compare two strings.

printf("Enter the first string: ");
fgets(str1, SIZE, stdin);

printf("Enter the second string: ");
fgets(str2, SIZE, stdin);

int result = strcmp(str1, str2);
if (result == 0) {
    printf("Strings are equal.\n");
} else if (result < 0) {
    printf("First string is less than the second string.\n");
} else {
    printf("First string is greater than the second string.\n");
}

Step 8: Test the program by running it and providing the required inputs.

That's it! You have successfully written a C program using string functions to perform the mentioned functionalities.

This problem has been solved

Similar Questions

What is the function to concatenate two strings in C programming?

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()

A set of simple string manipulation functions are employed in _______a.<stdio.h>b.<conio.h>c.<string.h>d.<math.h>

What is string concatenation in C programming?

What is the function to compare two strings lexicographically in C programming?

1/3

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.