[英]Return value don't return
I wrote a word_pattern function as below.我写了一个word_pattern函数,如下所示。
I didn't get a return value, what's wrong with the code?我没有得到返回值,代码有什么问题?
Below is the codewars training.( https://www.codewars.com/kata/5f3142b3a28d9b002ef58f5e/train/c )以下是codewars培训。( https://www.codewars.com/kata/5f3142b3a28d9b002ef58f5e/train/c )
#include <stdlib.h>
#include <string.h> //strlen
#include <ctype.h> //islower
char *word_pattern(const char *word) {
int i, j;
int size = strlen(word) ;
char *ans = (char*)malloc(sizeof(char) * (size * 2 ));
char *base = ans;
if (ans == NULL)
{
return NULL;
}
for (i = 0; i < size; i++)
{
if (i == 0)
{
*ans++ = (char)i;
*ans++ = '.';
/*break;*/ continue;
}
for (j = 0; j < i; j++)
{
if ( islower(word[i]) == islower(word[j]) )
{
*ans++ = (char)j;
*ans++ = '.';
break;
}
}
if(j == i)
{
*ans++ = (char)i;
*ans++ = '.';
}
}
*--ans = '\0';
return base;
}
I didn't get a return value, what's wrong with the code?我没有得到返回值,代码有什么问题?
Below code sets ans[0]
to 0, the null character .下面的代码将ans[0]
为 0,即空字符。
if (i == 0) {
*ans++ = (char)i;
Printing the return pointer as a string prints nothing as the string ends immediately.将返回指针打印为字符串不会打印任何内容,因为字符串会立即结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.