Write a function that concatenates two strings.Prototype: char *string_nconcat(char *s1, char *s2, unsigned int n);The returned pointer shall point to a newly allocated space in memory, which contains s1, followed by the first n bytes of s2, and null terminatedIf the function fails, it should return NULLIf n is greater or equal to the length of s2 then use the entire string s2if NULL is passed, treat it as an empty string
Question
Write a function that concatenates two strings.Prototype: char *string_nconcat(char *s1, char *s2, unsigned int n);The returned pointer shall point to a newly allocated space in memory, which contains s1, followed by the first n bytes of s2, and null terminatedIf the function fails, it should return NULLIf n is greater or equal to the length of s2 then use the entire string s2if NULL is passed, treat it as an empty string
Solution
Here is a function in C that fulfills your requirements:
#include <stdlib.h>
#include <string.h>
char *string_nconcat(char *s1, char *s2, unsigned int n)
{
char *concat_str;
unsigned int len_s1 = 0, len_s2 = 0, i;
if (s1 == NULL)
s1 = "";
if (s2 == NULL)
s2 = "";
while (s1[len_s1])
len_s1++;
while (s2[len_s2])
len_s2++;
if (n >= len_s2)
n = len_s2;
concat_str = malloc(sizeof(char) * (len_s1 + n + 1));
if (concat_str == NULL)
return NULL;
for (i = 0; i < len_s1; i++)
concat_str[i] = s1[i];
for (i = 0; i < n; i++)
concat_str[i + len_s1] = s2[i];
concat_str[len_s1 + n] = '\0';
return concat_str;
}
This function first checks if either of the input strings are NULL, and if so, treats them as empty strings. It then calculates the lengths of both strings. If n is greater than or equal to the length of s2, it sets n to be the length of s2.
It then allocates memory for the new string, which will be the size of s1 plus n plus 1 (for the null terminator). If memory allocation fails, it returns NULL.
The function then copies the characters from s1 and the first n characters from s2 into the new string. Finally, it adds a null terminator to the end of the new string and returns it.
Similar Questions
What is the function to concatenate two strings in C programming?
What is string concatenation in C programming?
How do you concatenate two strings s1 and s2 in C++?s1 + s2s1.append(s2)strcat(s1, s2)Both a) and b)
Which of the following functions is used to concatenate two strings in C?strcat;strappend()strcat()strjoin()strmerge()
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 OutputConcatenated string: VelloreChennaiLength of the string1: 7String1: ChennaiSize of string2: 50String1 and String2 are not equal
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.