Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用循环反转C中的字符串_C_Cs50 - Fatal编程技术网

使用循环反转C中的字符串

使用循环反转C中的字符串,c,cs50,C,Cs50,初学者程序员在这里。我试图从用户那里获取输入,将其反转并显示结果。出于某种原因,它打印的是空白而不是反向字符串。我知道数组[I]有正确的信息,因为如果我在int I=0的行上使用这个循环;我 作为旁注,在这个小练习中使用VLA可能意义不大,但对于较大的输入,它很可能会溢出调用堆栈。当心 // the header where strlen is #include <string.h> /** * \brief reverse the string pointed by str *

初学者程序员在这里。我试图从用户那里获取输入,将其反转并显示结果。出于某种原因,它打印的是空白而不是反向字符串。我知道数组[I]有正确的信息,因为如果我在int I=0的行上使用这个循环;我 作为旁注,在这个小练习中使用VLA可能意义不大,但对于较大的输入,它很可能会溢出调用堆栈。当心

// the header where strlen is
#include <string.h>

/**
 * \brief reverse the string pointed by str
**/
void reverseString(char* str) {
    int len = strlen(str);
    // the pointer for the left and right character
    char* pl = str;
    char* pr = str+len-1;
    // iterate to the middle of the string from left and right (len>>1 == len/2)
    for(int i = len>>1; i; --i, ++pl, --pr) {
        // swap the left and right character
        char l = *pl;
        *pl = *pr;
        *pr = l;
    };
};
换衣服就行了

char array[count];

for (int i=0; i< count; i++)
{
    array[i] = word[i];
}

你的情况不对。它应该是:对于int i=count-1;i> =0;把输入一字不差地复制到别的地方有什么意义?为什么不直接从它所在的位置打印它呢?如果你想反转字符串,你的第一个for循环必须改变。一个数组对其他数组进行计数down@P.P.谢谢你,我不知道为什么我没有想到它。@DavidSchwartz我一开始在一个数组中完成了所有操作,为了更好地调试它,我将它分解了。而且你忘记了空终止字符,缓冲区太短了1@Jean-弗朗索瓦·法布——是吗?如果这就是你投反对票的原因,那么你真的是个导火索happy@Jean-弗朗索瓦·法布他没有使用终止符,他的缓冲区也不太短。缓冲区保存长度已知的字符,而不是C样式的字符串。没有必要再分配一个字节。@Jean-Françoisfare-很容易犯错误,在不到15秒的时间里,先进行否决表决,然后再进行评论。道歉。@Jean-Françoisfare-这有点像鸡和蛋的事情。我通常在迭代中不断改进,直到我满意为止。但我不能证明不是DV让我这么做的。不管怎样,DV仍然存在的事实强烈地表明它不是一个真实的DV。没有任何解释的代码是不会有任何帮助的。你应该在编写汇编代码的时候发布汇编代码。好吧,但是有很多代码片段是这样做的,所以请遵循我的重复链接。OP想知道他代码中的问题。
char array[count + 1];
array[count] = '\0';

for (int i = 0; i< count; i++)
{
    array[i] = word[count - i];
}
// the header where strlen is
#include <string.h>

/**
 * \brief reverse the string pointed by str
**/
void reverseString(char* str) {
    int len = strlen(str);
    // the pointer for the left and right character
    char* pl = str;
    char* pr = str+len-1;
    // iterate to the middle of the string from left and right (len>>1 == len/2)
    for(int i = len>>1; i; --i, ++pl, --pr) {
        // swap the left and right character
        char l = *pl;
        *pl = *pr;
        *pr = l;
    };
};
int main(void) {
    printf("Please enter a word: ");
    char *word = get_string();

    // Just call the function. Note: the memory is changed, if you want to have the original and the reversed just use a buffer and copy it with srcpy before the call
    reverseString(word)
    printf("%s\n", word);
};
char array[count];

for (int i=0; i< count; i++)
{
    array[i] = word[i];
}
// add an other byte for the null-terminating character!!!
char array[count+1];
strcpy(array, word);