将单个字符从字符数组复制到 C 中的另一个字符数组
Copy a single character from a character array to another character array in C
我对 C 编程还很陌生,正在努力改进。我看到了一些与此类似的问题,并尝试使用 strncpy 实施他们的建议,但它仍然无法正常工作。谁能给我一个正确方向的指示?谢谢
代码的目的是给定一个字符串并在找到空白时将其拆分 space。我故意不使用 strtok()
。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[10] ;
char word[10] ;
int i, j, k, l ;
int main()
{
printf("Please enter a string of ten characters and I'll find the blanks\n") ;
fgets(str,10,stdin) ;
printf("Original string is %s\n", str) ;
j = 0 ;
for(i == 0 ; i < 10 ; i++){
if(!isblank(str[i])){
strncpy(word[j], &str[i], 1) ;
printf("i = %d, j = %d, str[i] = %c, word[j] = %c\n",i, j, str[i], word[j]) ;
j++ ;
}else{
printf("%s\n",word) ;
j = 0 ;
}
}
return 0 ;
}
在我看来,问题出在
for(i == 0 ; i < 10 ; i++)
^^
您应该使用作业 (=
),例如
for(i = 0 ; i < 10 ; i++)
设置i
的初始值。否则,通过访问未初始化局部变量的值(其值在没有初始化的情况下是不确定的),您的代码将调用 undefined behavior.
然后,strncpy()
的用法看起来也不对,您没有按照第一个参数的要求传递 char *
。
那么,还有两件事,
要一次复制一个 char
,您可以简单地使用赋值,例如
word[j] = str[i];
您应该只循环遍历输入数组直到有效条目,而不是整个大小 (10)。如果输入较小,您将再次点击 UB。
我认为你的问题是没有指向 strncpy 函数中的 dest 参数的指针。
strncpy(word[j], &str[i], 1) ;
^
为 strncpy 函数添加指向目标的指针
strncpy(&word[j], &str[i], 1) ;
以及解决 Sourav Ghosh 提到的问题,你应该拥有它。
正如我在@James 上评论的那样,这在没有 strsep() 的非 BSD 上是一个真正的问题。
strtok() 和 strtok_r() 都是 IMO 的根本缺陷,因为它们单方面跳过任何 >1 个相邻分隔符的序列。
我使用了这个解决方案:
https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/
为了用 space 填补上面的空白,这样我就可以对我的所有有效负载进行适当的标记化。
Reader 当心,上面的解决方案有一个缺陷......所以你可能需要多次调用它......不是最优的,但如果你像我一样匆忙,我使用它并且它有效。
while(strstr(token, ";;")!=NULL) {
token = replaceWord(token, ";;", "; ;");
(void) sprintf (v_sg_TempMsg, "Massaged the input agsId to token:[%s]", token);
printf(v_sg_TempMsg);
++tokenCounter;
if(tokenCounter==15) {
break;
}
}//while(strstr(token, ";;")!=NULL) {
...以上是调用有缺陷的代码以最终删除所有分隔符的代码...这是肮脏的肮脏代码,所以请不要煽动...它是在这里帮助那些因为缺少替代 strtok() 和 strtok_r() 跳过而被封锁的人。
我对 C 编程还很陌生,正在努力改进。我看到了一些与此类似的问题,并尝试使用 strncpy 实施他们的建议,但它仍然无法正常工作。谁能给我一个正确方向的指示?谢谢
代码的目的是给定一个字符串并在找到空白时将其拆分 space。我故意不使用 strtok()
。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[10] ;
char word[10] ;
int i, j, k, l ;
int main()
{
printf("Please enter a string of ten characters and I'll find the blanks\n") ;
fgets(str,10,stdin) ;
printf("Original string is %s\n", str) ;
j = 0 ;
for(i == 0 ; i < 10 ; i++){
if(!isblank(str[i])){
strncpy(word[j], &str[i], 1) ;
printf("i = %d, j = %d, str[i] = %c, word[j] = %c\n",i, j, str[i], word[j]) ;
j++ ;
}else{
printf("%s\n",word) ;
j = 0 ;
}
}
return 0 ;
}
在我看来,问题出在
for(i == 0 ; i < 10 ; i++)
^^
您应该使用作业 (=
),例如
for(i = 0 ; i < 10 ; i++)
设置i
的初始值。否则,通过访问未初始化局部变量的值(其值在没有初始化的情况下是不确定的),您的代码将调用 undefined behavior.
然后,strncpy()
的用法看起来也不对,您没有按照第一个参数的要求传递 char *
。
那么,还有两件事,
要一次复制一个
char
,您可以简单地使用赋值,例如word[j] = str[i];
您应该只循环遍历输入数组直到有效条目,而不是整个大小 (10)。如果输入较小,您将再次点击 UB。
我认为你的问题是没有指向 strncpy 函数中的 dest 参数的指针。
strncpy(word[j], &str[i], 1) ;
^
为 strncpy 函数添加指向目标的指针
strncpy(&word[j], &str[i], 1) ;
以及解决 Sourav Ghosh 提到的问题,你应该拥有它。
正如我在@James 上评论的那样,这在没有 strsep() 的非 BSD 上是一个真正的问题。 strtok() 和 strtok_r() 都是 IMO 的根本缺陷,因为它们单方面跳过任何 >1 个相邻分隔符的序列。
我使用了这个解决方案: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/ 为了用 space 填补上面的空白,这样我就可以对我的所有有效负载进行适当的标记化。 Reader 当心,上面的解决方案有一个缺陷......所以你可能需要多次调用它......不是最优的,但如果你像我一样匆忙,我使用它并且它有效。
while(strstr(token, ";;")!=NULL) {
token = replaceWord(token, ";;", "; ;");
(void) sprintf (v_sg_TempMsg, "Massaged the input agsId to token:[%s]", token);
printf(v_sg_TempMsg);
++tokenCounter;
if(tokenCounter==15) {
break;
}
}//while(strstr(token, ";;")!=NULL) {
...以上是调用有缺陷的代码以最终删除所有分隔符的代码...这是肮脏的肮脏代码,所以请不要煽动...它是在这里帮助那些因为缺少替代 strtok() 和 strtok_r() 跳过而被封锁的人。