[英]Bubble Sorting Strings According to Upper Case and Lower Case Letters
I am trying to bubblier sort my 2d array. 我试图让我的2d数组冒泡。 Once I get to this point I get memory leaks:
一旦达到这一点,我就会出现内存泄漏:
te = word[i];
word[i] = word[i+1];
Once I get to this point I get a segment fault: 一旦达到这一点,我就会遇到段错误:
word[i+1]=te;
I have tried using strcpy
to copy the strings but I still get the segment fault error when I use it too. 我尝试使用
strcpy
复制字符串,但是当我也使用它时,仍然出现段错误错误。
void word_sorter(char** word,int numWord){
int i =0;
int j =0;
for(i = 0; i < numWord; i++){
for(j = 0; word[i][j] != '\0'; j++){
if(word[i][j]>= 'A' && word[i][j] <= 'Z'){
char *te;
te =(char*)calloc(10000,sizeof(char));
te = word[i];
word[i] = word[i+1];
word[i+1]=te;
}
}
}
}
I am trying to sort the strings so that capital letters have precedence over lower case letter 我正在尝试对字符串进行排序,以便大写字母优先于小写字母
INPUT: aand AAnd Aand
OUTPUT:AAnd Aand aand
You're allocating memory and assigning that address to te
, then immediately overwriting that value with te = word[i]
. 您正在分配内存并将该地址分配给
te
,然后立即用te = word[i]
覆盖该值。 This will leak the memory returned by calloc
. 这将泄漏
calloc
返回的内存。
word[i+1]
will access past the end of the array on the last iteration of the i
loop, when i == numWord - 1
. 当
i == numWord - 1
时, word[i+1]
将在i
循环的最后一次迭代中访问数组末尾。 This is the source of your segmentation fault. 这是您的细分错误的根源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.