Write a function that prints strings, followed by a new line.Prototype: void print_strings(const char *separator, const unsigned int n, ...);where separator is the string to be printed between the stringsand n is the number of strings passed to the functionYou are allowed to use printfIf separator is NULL, don’t print itIf one of the string is NULL, print (nil) insteadPrint a new line at the end of your functionjulien@ubuntu:~/0x0f. Variadic functions$ cat 2-main.c#include "variadic_functions.h"/** * main - check the code * * Return: Always 0. */int main(void){ print_strings(", ", 2, "Jay", "Django"); return (0);}julien@ubuntu:~/0x0f. Variadic functions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-print_strings.c -o cjulien@ubuntu:~/0x0f. Variadic functions$ ./c Jay, Djangojulien@ubuntu:~/0x0f. Variadic functions$
Question
Write a function that prints strings, followed by a new line.Prototype: void print_strings(const char *separator, const unsigned int n, ...);where separator is the string to be printed between the stringsand n is the number of strings passed to the functionYou are allowed to use printfIf separator is NULL, don’t print itIf one of the string is NULL, print (nil) insteadPrint a new line at the end of your functionjulien@ubuntu:~/0x0f. Variadic functions cat 2-main.c#include "variadic_functions.h"/** * main - check the code * * Return: Always 0. */int main(void){ print_strings(", ", 2, "Jay", "Django"); return (0);}julien@ubuntu:~/0x0f. Variadic functions gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-print_strings.c -o cjulien@ubuntu:~/0x0f. Variadic functions
Solution
Here is a C function that fulfills your requirements:
#include <stdarg.h>
#include <stdio.h>
void print_strings(const char *separator, const unsigned int n, ...)
{
va_list args;
unsigned int i;
char *str;
va_start(args, n);
for (i = 0; i < n; i++)
{
str = va_arg(args, char*);
if (str == NULL)
printf("(nil)");
else
printf("%s", str);
if (i != n - 1 && separator != NULL)
printf("%s", separator);
}
printf("\n");
va_end(args);
}
This function uses the stdarg.h library to handle a variable number of arguments. It starts by initializing a va_list and then enters a loop that will go through each argument. If the current argument is NULL, it prints (nil). Otherwise, it prints the string. If the current argument is not the last one and the separator is not NULL, it prints the separator. After going through all the arguments, it prints a new line and ends the va_list.
Similar Questions
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
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.
In this challenge, we practice reading input from stdin and printing output to stdout.In C++, you can read a single whitespace-separated token of input using cin, and print output to stdout using cout. For example, let's say we declare the following variables:string s;int n;
What is the function to concatenate two strings in C programming?
Which function is used to print a message on the console in C++?Select one:cout<<;cin>>;Printf()printf();
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.