尝试将 c 字符串数组与分隔符连接起来
Trying to concatenate array of c-strings with delimiter
这是我的代码
int main(int argc, char *argv[]) {
char const *strings[10] = {"dhh", "aci", "cdh"};
join_def(strings, 'l');
return EXIT_SUCCESS;
}
// part 1 read lines
void join_def(char const **strings, char delim) {
char *t = new char[100];
//int length = 0;
t[0] = ' ';
int x = sizeof(strings);
std::cout << delim << std::endl;
for (int i = 0; i < x; i++) {
int size = 0;
while(strings[i][size]!=' '){
size++;
std::cout << strings[i][size] << std::endl;
}
}
}
我已经花了几个小时了,我只是无法连接它 对于此任务,我不能使用 cstring 或 iostream 以外的任何东西,所以请不要建议。
输出需要是一个 c 字符串 = "dhhlacilcdh">
首先,您无法确定传递给函数的数组中的元素数,因为该数组将衰减为简单的指针。因此,您的sizeof(strings)
表达式将(在编译时(计算指针的(固定(大小(以字节为单位(。为了使函数"知道"数组中有多少元素,需要显式告诉它(通过额外的参数(。
其次,您的i
' 和size
索引在std::cout << strings[i][size] << std::endl;
行中以错误的方式进行,此外,您在打印相关字符之前递增size
,而在打印后应该递增。
下面的代码还执行字符串的实际连接,修改后的join_def
函数现在返回指向该结果的指针(完成后必须释放该指针(;
#include <iostream>
char* join_def(char const** strings, char delim, int x)
{
char* t = new char[100];
int length = 0;
t[0] = ' ';
//int x = sizeof(strings);
std::cout << delim << std::endl;
for (int i = 0; i < x; i++) {
int size = 0;
while (strings[i][size] != ' ') {
std::cout << strings[i][size] << std::endl;
t[length++] = strings[i][size]; // Append this character
size++;
}
t[length++] = delim; // Append delimiter
}
t[length] = ' '; // Append nul-terminator
return t;
}
int main()
{
char const* strings[10] = { "dhh", "aci", "cdh" };
char* result = join_def(strings, 'l', 3);
std::cout << result << std::endl;
free(result);
return 0;
}
另请注意,我已将join_def
函数代码移动到main
(调用它(之前。如果你不这样做,那么至少必须在main之前提供该函数的(前向(声明(只是一个char* join_def(char const** strings, char delim, int x);
本身就可以了(。
请随时要求进一步澄清和/或解释。
我不确定你想做什么,但也许这会有所帮助?
#include <iostream>
// part 1 read lines
void join_def(char const **strings, char delim)
{
char *t = new char[100];
//int length = 0;
t[0] = ' ';
int x = 0;
for (int i = 0; strings[i] != nullptr; i++)
x += sizeof(strings[i]) - 1;
std::cout << delim << std::endl;
for (int i = 0; strings[i] != nullptr; i++)
{
int size = 0;
while (strings[i][size] != ' ')
{
size++;
std::cout << strings[i][size] << std::endl;
}
}
}
int main(int argc, char *argv[])
{
char const *strings[] = {"dhh", "aci", "cdh", nullptr};
join_def(strings, 'l');
return EXIT_SUCCESS;
}
这就是你要找的吗? 看起来我删除了所有std::endl
,因为它喜欢'n'
我也在std::cout
之后移动了你的size++
#include <iostream>
// part 1 read lines
void join_def(char const **strings, char delim,int length) {
char *t = new char[100];
//int length = 0;
t[0] = ' ';
int x = length;
for (int i = 0; i < x; i++) {
int size = 0;
while(strings[i][size]!=' '){
std::cout << strings[i][size]; //<--print befure increment size
size++;
}
std::cout << delim;
}
}
int main(int argc, char *argv[]) {
char const *strings[] = {"dhh", "aci", "cdh"};
join_def(strings,'|',3); //<- need to send the length of the char* array
return EXIT_SUCCESS;
}
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 当用户键入分隔符时,停止getline()输入
- 无法在windows上使用mingw将sqlite3与c连接
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 带有多个字符分隔符的正则表达式
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 尝试将 c 字符串数组与分隔符连接起来
- 如何使用分隔符连接命令行参数
- 范围-v3 如何操作::使用分隔符连接