好的,所以我编写了一个小函数来将字符串中的任何大写字母转换为小写字母,只是为了从我正在学习 C 的书中做一个练习。
除了通过指针将值分配给“字符”之外,一切正常。
这是代码,一切都正确编译,但我收到此运行时错误“未知的伪重定位协议版本 %d”。这就是为什么我尝试打印通过指针更改了值的字符的原因。
#include <stdlib.h>
#include <stdio.h>
/*
----------------------------------------------
CONVERTS UPPERCASE CHARACTERS TO LOWERCASE
----------------------------------------------
*/
void lowercase(char * address, char text2){
// used in the for loop
int inc;
// used as an index for text2Copy
int inctwo = 0;
// used in the for loop
int length = strlen(text2);
//used to copy the active character in text2
char text2Copy[length];
for(inc = 0; inc <= length; inc++){
//basicaly if character is a capital leter
if(text2[inc] >= 'A' && text2[inc] <= 'Z'){
//I plus 32 because each letter is 32 numbers away in 'ASCII'
//therefore converting capital to lowercase
text2Copy[inctwo] = text2[inc] + 32;
//add one to help with indexing
inctwo++;
}
//if the character is not a capital leter
else{
text2Copy[inctwo] = text2[inc];
inctwo++;
}
}
//*address = "sdafsdf"; //<-- THIS WORKS!!!
*address = text2Copy;//WHY DOESN"T THIS WORK?
}
int main(){
//just the string I will be using.
char * text = "'CONVERT capitals TO lower CASE'";
//print the string to show the original
printf("%s\n",text);
lowercase(&text,text);
//This is where I want the value from the function to print out
printf("%s\n",text);
return 0;
}
如果你能帮助我,我将不胜感激,我真的很困惑,而且对为什么这不起作用有点恼火。如果您需要我更好地解释它,请请求它,我希望我已经做得足够了。
谢谢,杰克。
///////////////////////////////////////// ////编辑///////////////////////////////////////////// ////////////
好的,我已经使用了您的所有建议,谢谢:D
,现在它除了一个我不知道如何修复的奇怪错误之外,它目前可以正常工作。
除了第一个字符之外的所有内容都变成了小写字符。
现在发生了什么->“+将大写转换为小写”我不知道为什么第一个字符会这样做?有什么想法吗?
这是新代码。
#include <stdlib.h>
#include <stdio.h>
/*
----------------------------------------------
CONVERTS UPPERCASE CHARACTERS TO LOWERCASE
----------------------------------------------
*/
void lowercase(char * address, char text2[]){
// used in the for loop
int inc;
// used in the for loop
int length = strlen(text2);
for(inc = 0; inc <= length; inc++){
//basicaly if character is a capital leter
if(text2[inc] >= 'A' && text2[inc] <= 'Z'){
//I plus 32 because each letter is 32 numbers away in 'ASCII'
//therefore converting capital to lowercase
text2[inc] += 32;
//add one to help with indexing
inctwo++;
}
//if the character is not a capital leter
else{
inctwo++;
}
}
*address = text2;
}
int main(){
//just the string I will be using.
char text[] = "cONVERT capitals TO lower CASE";
//print the string to show the original
printf("%s\n",text);
lowercase(&text,text);
//This is where I want the value from the function to print out
printf("%s\n",text);
return 0;
}