我在用C语言工作,我必须把一些东西连接起来。
现在我有这个:
message = strcat("TEXT ", var);
message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));
现在,如果你有C语言的经验,我相信你会意识到,当你试图运行它时,这会给你一个分割错误。我该怎么做呢?
我在用C语言工作,我必须把一些东西连接起来。
现在我有这个:
message = strcat("TEXT ", var);
message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));
现在,如果你有C语言的经验,我相信你会意识到,当你试图运行它时,这会给你一个分割错误。我该怎么做呢?
当前回答
int main()
{
char input[100];
gets(input);
char str[101];
strcpy(str, " ");
strcat(str, input);
char *p = str;
while(*p) {
if(*p == ' ' && isalpha(*(p+1)) != 0)
printf("%c",*(p+1));
p++;
}
return 0;
}
其他回答
试试类似的方法:
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[])
{
// Insert code here...
char firstname[100], secondname[100];
printf("Enter First Name: ");
fgets(firstname, 100, stdin);
printf("Enter Second Name: ");
fgets(secondname,100,stdin);
firstname[strlen(firstname)-1]= '\0';
printf("fullname is %s %s", firstname, secondname);
return 0;
}
另外,如果您不知道要连接多少字符串,malloc和realloc也很有用。
#include <stdio.h>
#include <string.h>
void example(const char *header, const char **words, size_t num_words)
{
size_t message_len = strlen(header) + 1; /* + 1 for terminating NULL */
char *message = (char*) malloc(message_len);
strncat(message, header, message_len);
for(int i = 0; i < num_words; ++i)
{
message_len += 1 + strlen(words[i]); /* 1 + for separator ';' */
message = (char*) realloc(message, message_len);
strncat(strncat(message, ";", message_len), words[i], message_len);
}
puts(message);
free(message);
}
您正在尝试将字符串复制到静态分配的地址中。你需要进入缓冲地带。
具体地说:
剪断…
目的地
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
剪断…
http://www.cplusplus.com/reference/clibrary/cstring/strcat.html
这里也有一个例子。
正如人们指出的,字符串处理改进了很多。所以你可能想学习如何使用c++字符串库而不是C风格的字符串。但是这里有一个纯C的解
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void appendToHello(const char *s) {
const char *const hello = "hello ";
const size_t sLength = strlen(s);
const size_t helloLength = strlen(hello);
const size_t totalLength = sLength + helloLength;
char *const strBuf = malloc(totalLength + 1);
if (strBuf == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
strcpy(strBuf, hello);
strcpy(strBuf + helloLength, s);
puts(strBuf);
free(strBuf);
}
int main (void) {
appendToHello("blah blah");
return 0;
}
我不确定它是否正确/安全,但现在我找不到更好的方法来做到这一点在ANSI C。
在C语言中,“字符串”只是简单的字符数组。因此,您不能直接将它们与其他“字符串”连接。
你可以使用strcat函数,它将src指向的字符串追加到dest指向的字符串的末尾:
char *strcat(char *dest, const char *src);
下面是一个来自cplusplus.com的例子:
char str[80];
strcpy(str, "these ");
strcat(str, "strings ");
strcat(str, "are ");
strcat(str, "concatenated.");
对于第一个参数,需要提供目标缓冲区本身。目标缓冲区必须是字符数组缓冲区。例如:char buffer[1024];
确保第一个参数有足够的空间存储要复制的内容。如果可以,使用像:strcpy_s和strcat_s这样的函数更安全,其中必须显式指定目标缓冲区的大小。
注意:字符串字面值不能用作缓冲区,因为它是常量。因此,您总是必须为缓冲区分配一个char数组。
strcat的返回值可以简单地忽略,它只返回与作为第一个参数传入的指针相同的指针。它的存在是为了方便,并允许你将调用链接到一行代码中:
strcat(strcat(str, foo), bar);
所以你的问题可以这样解决:
char *foo = "foo";
char *bar = "bar";
char str[80];
strcpy(str, "TEXT ");
strcat(str, foo);
strcat(str, bar);