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.Write each functionality as different function and call it from the main program.

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.

🧐 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: Define the maximum array size:

#define MAX_SIZE 50

Step 3: Declare the functions for each functionality:

void concatenateStrings(char[], char[]);
int findStringLength(char[]);
void copyString(char[], char[]);
int findStringSize(char[]);
int compareStrings(char[], char[]);

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("String 1 is less than String 2\n");
    } else {
        printf("String 1 is greater than String 2\n");
    }
    return result;
}

Step 9: Write the main function to call the above functions:

int main() {
    char str1[MAX_SIZE], str2[MAX_SIZE];
    
    printf("Enter the first string: ");
    fgets(str1, sizeof(str1), stdin);
    
    printf("Enter the second string: ");
    fgets(str2, sizeof(str2), stdin);
    
    concatenateStrings(str1, str2);
    findStringLength(str1);
    copyString(str1, str2);
    findStringSize(str1);
    compareStrings(str1, str2);
    
    return 0;
}

That's it! You have now implemented a C program that performs the mentioned functionalities using string functions.

This problem has been solved

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 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.