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.Sample InputEnter String1: VelloreEnter String2: ChennaiSample Output

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: ChennaiSample Output

...expand
🧐 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:

  1. Start by including the necessary header file for string functions:
#include <stdio.h>
#include <string.h>
  1. Define the maximum size of the array as 50:
#define MAX_SIZE 50
  1. 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[]);
  1. Implement the function to concatenate two strings:
void concatenateStrings(char str1[], char str2[]) {
    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);
}
  1. 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;
}
  1. Implement the function to copy one string to another:
void copyString(char str1[], char str2[]) {
    strcpy(str1, str2);
    printf("Copied string: %s\n", str1);
}
  1. 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;
}
  1. 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;
}
  1. Finally, implement the main function to call these functionalities:
int main() {
    char str1[MAX_SIZE], str2[MAX_SIZE];

    printf("Enter String1: ");
    fgets(str1, sizeof(str1), stdin);

    printf("Enter String2: ");
    fgets(str2, sizeof(str2), 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;
}

This program prompts the user to enter two strings, performs the mentioned functionalities using the defined functions, and displays the results accordingly.

Note: The fgets function is used to read the input strings to handle spaces and avoid buffer overflow. The strcspn function is used to remove the newline character from the input strings.

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>

What is string concatenation in C programming?

What is the function to compare two strings lexicographically 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()

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.