Tag: string

在C中反转一个字符串

我知道这已被问过几千次,但我在代码中找不到错误。 有人可以指出我做错了什么吗? #include #include void reverseString(char *myString){ char temp; int len = strlen(myString); char *left = myString; // char *right = &myString[len-1]; char *right = myString + strlen(myString) – 1; while(left < right){ temp = *left; *left = *right; // this line seems to be causing a segfault *right = temp; left++; right–; } } […]

用scanf读取C ++字符串

正如标题所说,我很好奇是否有办法用scanf读取C ++字符串。 我知道我可以读取每个char并将其插入到deserved字符串中,但我想要的是: string a; scanf(“%SOMETHING”, &a); gets()也行不通。 提前致谢!

使用scanf读取带空格的字符串?

我希望以下内容要求输入,然后接受一个字符串(带空格),然后再做一次。 但在输入第一个字符串后,它会重复输出“input $”。 char command[80]; while(1) { printf(“input$ “); scanf(“%[^\n]”, command); } 我的输出: nput$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ […]

是否存在strtol等价物,不需要以空字符结尾的字符串?

是否存在类似于strtol的标准C函数,它将采用char *和非空终止字符串的长度? 我知道我可以将字符串复制到以null结尾的区域,但出于效率原因这是不可取的。 谢谢。

strtok不接受:char * str

当使用char * str作为第一个参数(而不是分隔符字符串)时,strtok将无法正常工作。 它是否与以该表示法分配字符串的区域有关? (据我所知,这是一个只读区域)。 提前致谢 例: //char* str =”- This, a sample string.”; // <—doesn't work char str[] ="- This, a sample string."; // <—works char delims[] = " "; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,delims); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, […]

比较两个C风格字符串的正确function是什么?

所以我陷入两难境地。 我需要比较两个C风格的字符串,我搜索了最合适的函数: memcmp //Compare two blocks of memory (function) strcmp //Compare two strings (function ) strcoll //Compare two strings using locale (function) strncmp //Compare characters of two strings (function) strxfrm //Transform string using locale (function) 我认为第一个是地址,所以这个想法就出来了。 第二个听起来对我来说是最好的选择,但无论如何我想听到反馈。 其他三个让我无能为力。

使用预处理器进行字符串连接

是否可以在预处理期间连接字符串? 我找到了这个例子 #define H “Hello ” #define W “World!” #define HW HW printf(HW); // Prints “Hello World!” 但它对我不起作用 – 当我使用gcc -std=c99时打印出“Hello” UPD这个例子现在看起来像是在工作。 但是,它是c预处理器的常规function吗?

我可以从C ++字符串中获取非const C字符串吗?

C ++中的const-correctness仍让我感到头疼。 在使用一些旧的C代码时,我发现自己需要将C ++字符串对象转换为C字符串并将其分配给变量。 但是,变量是char * , c_str()返回const char [] 。 有没有一个很好的方法来解决这个问题,而不必自己动手去做呢? 编辑:我也试图避免调用新的。 我很乐意交换稍微复杂的代码以减少内存泄漏。

“以零结束”是什么意思?

我正在进入C / C ++,很多术语对我来说都是不熟悉的。 其中一个是由零终止的变量或指针。 内存空间被零终止是什么意思?

整数到IP地址 – C.

我正在准备一个测验,我怀疑我可能会负责实现这样的function。 基本上,给定一个网络符号的IP地址,我们如何从一个32位整数到它的点分十进制表示法(如155.247.182.83)…? 显然我们也不能使用任何类型的inet函数……我很难过!