Tag: 字符串长度

在C中,按字符串长度排序字符串数组

所以我将字符串输入到数组mydata[10][81] while ((ct<=10) && gets(mydata[ct]) != NULL && (mydata[ct++][0] != '\0')) 然后我使用for循环来创建第二个指针数组 for (i=0;i<11;i++){ ptstr[i] = mydata[i]; } 这是我卡住的地方我知道我需要以某种方式使用strlen ,但我甚至无法想到如何获得指针的长度然后根据第三个额外的长度值重新指定该指针的新位置 希望这是有道理的,我很失落如何做或解释它,我只是尝试使用数组位置按长度排序字符串(不使用像qsort这样的东西) 我做了一些更多的工作并想出了这个:任何想法为什么它不起作用? void orderLength(char *ptstr[], int num){ int temp; char *tempptr; int lengthArray[10]; int length = num; int step, i, j, u; for (i=0; i<num;i++){ lengthArray[i] = strlen(ptstr[i]); } for (step=0; step < length; step++){ […]

如何使用fgets读取未知长度的输入

我怎么应该使用fgets()读取长输入,我不太明白。 我写了这个 #include #include #include int main() { char buffer[10]; char *input; while (fgets(buffer,10,stdin)){ input = malloc(strlen(buffer)*sizeof(char)); strcpy(input,buffer); } printf(“%s [%d]”,input, (int)strlen(input)); free(input); return 0; }

预测sprintf()’ed line的len?

我可以用某个函数来预测sprintf()需要的空间吗? IOW,我可以调用一个函数size_t predict_space(“%s \ n”,some_string)来返回由sprintf(“%s \ n”,some_string)产生的C字符串的长度吗?

使用malloc分配时是否包含空字符

我已经使用C很长一段时间了,我有这个小问题,我想查询。 假设我想创建一个存储多达1000个字符的字符数组。 现在,当我使用malloc时,我将数组的大小指定为1001个字符[1000个字符+空]或仅1000个? 另外,说我遇到了这个问题,那么我怎么能自己找到这个解决方案的答案,也许是通过使用一些测试程序。 我理解字符串的大小是在没有空字符的情况下计算的,但是当我为它分配内存时,我是否也考虑了空字符?