gpt4 book ai didi

c - 程序 : String concatenation using concept of variable arugment functions

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:27 25 4
gpt4 key购买 nike

您好,我是初学者,我正在使用可变参数函数的概念来连接字符串。不同数量的字符串调用相同的函数。

我无法计算连接字符串的长度,这反过来意味着我没有正确分配内存。亲爱的同行们,请帮忙!

/* Program to do string concatenation using the concept of variable arguments */

/********************************************************************************
* REQUIRED HEADER FILES
*********************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>

/********************************************************************************
* REQUIRED MACROS DEFINED
*********************************************************************************/

#define ERROR_CHECKER(result)\
if(result == FAILURE)\
{\
printf("\n CONCATENATION FAILED");\
}
typedef enum {SUCCESS = 0, FAILURE = 1} return_type;

/********************************************************************************
* REQUIRED FUNCTION PROTOTYPES
*********************************************************************************/
return_type string_concat(char* string_pointer, ...);

/********************************************************************************
*
* FUNCTION_NAME : STRING_CONCAT
*
* DESCRIPTION : concatenates incoming strings and displays the result
*
* RETURNS : SUCCESS OR FAILURE
*
*********************************************************************************/
return_type string_concat(
char* string_pointer,
...)
{

/********************************************************************************
* REQUIRED DECLARATIONS
*********************************************************************************/
// 1. arg_list that will point to variable number of arguments
va_list arg_list;

// 2. pointer to concatenated string
char* concatenated_string;

// 3. character pointer to point to an individual element in the argument list
char* individual_string_pointer;

// 4. amount of memory required to be allocated
int length;
/*********************************************************************************
* REQUIRED INITIALIZATIONS
*********************************************************************************/
va_start(arg_list, string_pointer);
concatenated_string = NULL;
individual_string_pointer = string_pointer;
length = 0;
/*********************************************************************************
* PERFORMING REQUIRED TASKS
**********************************************************************************/

// 1. calculate length till you reach quit
while(strcmp(individual_string_pointer,"quit") == 0)
{
individual_string_pointer = va_arg(arg_list, char*);
length = length + strlen(individual_string_pointer);
}

// individual_string_pointer reinitialized to be used for concatenation
individual_string_pointer = string_pointer;

printf("\nlength of concatenated string : %d", length);

// 2. allocate memory for the concatenated string
concatenated_string = (char*) malloc(sizeof(char) * length + 1);

// 3. use strncpy to copy first string and then use strncat to concatenate others

strncpy(concatenated_string, string_pointer, sizeof(*(string_pointer)));

while(strcmp(individual_string_pointer, "quit") == 0)
{
individual_string_pointer = va_arg(arg_list, char*);
strncat(concatenated_string, individual_string_pointer, sizeof(*(individual_string_pointer)));
}

printf("\n concatenated string : %s",concatenated_string);

va_end(arg_list);
return SUCCESS;
}

/********************************************************************************
*
* FUNCTION_NAME : MAIN
*
* DESCRIPTION : CALLS STRING_CONCAT FUNCTION
*
* RETURNS : SUCCESS
*********************************************************************************/
int main(void)
{

/********************************************************************************
* REQUIRED DECLARATIONS
*********************************************************************************/
// 1. character array as the first argument
char string_one[5] = "hello" ;

// 2. variable to store result from the string_concat function.
int result;

/*********************************************************************************
* REQUIRED INITIALIZATIONS
**********************************************************************************/

result = 0;

/*********************************************************************************
* PERFORMING REQUIRED TASKS
**********************************************************************************/
// 1. call string_concat function with 2 arguments
result = string_concat(string_one, "my", "name","is","amninder","quit");
// handle error from string_concat
ERROR_CHECKER(result);

// 2. call string_concat function with 3 arguments
result = string_concat(string_one, "I", "Like","fruits","quit");
// handle error from string_concat
ERROR_CHECKER(result);

// 3. call string_concat function with 4 arguments
result = string_concat(string_one, "awesome","quit");
// handle error from string_concat
ERROR_CHECKER(result);
/* doubt: do I need to send my first argument as same always " */
return SUCCESS;
}

最佳答案

除其他问题外:此 sizeof(*(individual_string_pointer))); 返回 individual_string_pointer 指向的大小,即 char , 所以它返回 1。

要么改用 strlen(ndividual_string_pointer),要么改用 strcat(),如下所示:

strcat(concatenated_string, individual_string_pointer)

关于c - 程序 : String concatenation using concept of variable arugment functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41409846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com