gpt4 book ai didi

在 C 中构造字符串

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:40 24 4
gpt4 key购买 nike

这是我用来从字符数组构造字符串的演示代码,有没有更好的方法来构造字符串 *RV200# *FV200# ??

int main()
{
char String4H1[10] = "*FV";
char String4H3[10] = "*RV";
char String4H2[10] = "#";

char data1[10];
char data2[10];

snprintf(data1,4, "%03d", 200); //Convert integer to string function
snprintf(data2,4, "%03d", 200); //Convert integer to string function

ConvertToString(String4H1,data1, 3); //*FV200
ConvertToString(String4H3,data2, 3); //*RV200

ConvertToString(String4H1,String4H2,6); //*FV200#
ConvertToString(String4H3,String4H2,6); //*RV200#

//Display String4H1 And String 4H3

}


void ConvertToString(char subject[], const char insert[], int pos)
{
char buf[100] = {};
strncpy(buf, subject, pos); // copy at most first pos characters
int len = strlen(buf);
strcpy(buf+len, insert); // copy all of insert[] at the end
len += strlen(insert); // increase the length by length of insert[]
strcpy(buf+len, subject+pos); // copy the rest

strcpy(subject, buf); // copy it back to subject
// deallocate buf[] here, if used malloc()
}

200 在程序开始时是未知的,它是使用 IDE 函数从内存中获取的,以从特定内存地址获取值。像这样:-

unsigned short BUF = GetWord(@FrontVIB@,0);    
unsigned short BUF1 = GetWord(@RearVIB@,0);

//BUF and BUF1 stores the value of address @FrontVIB@ and @RearVIB@ respectively

**structure** :-
unsigned short GetWord( @Address Alias@, Address Offset );

最佳答案

这是一个简单的例子。可能我会被否决:)

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

bool concatenateString(char **dest, size_t *size, char *stringToAdd)
{
bool retVal = true;
char *dest_old = *dest;

*size += strlen(stringToAdd);

if (*dest == NULL)
{
*size += 1; // to add null terminator of string
*dest = calloc(1, size);
}
else
{
*dest = realloc(*dest, size);
}

if (dest == NULL)
{
free(dest_old);
retVal = false;
}
else
{
strcat(*dest, stringToAdd);
}

return retVal;
}

int main()
{
char newString[32] = {0};
int number;
size_t size = 0;

char *data1 = NULL;

printf("Insert a string:");
scanf(" %s", newString);
if (concatenateString(&data1, &size, newString))
{
printf("Insert a number:");
scanf(" %d", &number);
sprintf(newString, "%03d", number);
if (concatenateString(&data1, &size, newString) )
{
printf("Insert a string:");
scanf(" %s", newString);
if (concatenateString(&data1, &size, newString))
printf("%s\n", data1);
}
}

free(data1);
}

不使用动态分配

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

bool concatenateString(char *dest, size_t size_of_dest, char *stringToAdd)
{
bool retVal = true;

size_t new_size = strlen(dest) + strlen(stringToAdd);

if (new_size < size_of_dest)
{
strcat(dest, stringToAdd);
}
else
{
retVal = false;
}
return retVal;
}

int main()
{
char result[128] = {0};
char newString[32] = {0};
int number;

printf("Insert a string:");
scanf(" %s", newString);
printf("%s\n", newString);
if (concatenateString(result, sizeof(result), newString))
{
printf("Insert a number:");
scanf(" %d", &number);
sprintf(newString, "%03d", number);
if (concatenateString(result, sizeof(result), newString) )
{
printf("Insert a string:");
scanf(" %s", newString);
if (concatenateString(result, sizeof(result), newString))
printf("%s\n", result);
}
}
}

输入

Insert a string: *RV
Insert a number: 200
Insert a string: #

输出

*RV200#

关于在 C 中构造字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38792597/

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