首先我要为我的英语道歉,因为它有点meh,也是我第一次在网上问编程问题,所以很抱歉,如果我让它不清楚,所以,我的问题是,我需要一个代码,把空格之前,每个大写字母在字符串示例:AaaBbbCcC= Aaa Bbb Cc C但是,我在执行此操作时遇到错误***错误***分段错误(内核转储)。
//this function takes a string from int main at the end of the code
void function(char s[]){
int stringsize=0;
int a;
while(s[stringsize]!='\0'){
stringsize++;
}
//I made this while to find the size of the string given
stringsize++;
a=stringsize;
//I used 'a' to have two variables to store this data
//here I made a for to do the hard work and add a space before every capital letter, it simply sends everyone one house ahead and then put a space where it found the capital letter
for(int i=0; s[i]!='\0'; i++){
if(s[i]>='A'&&s[i]<='Z'){
while(a>i){
a--;
s[a+1]=s[a];
}
s[a]=' ';
//the code works perfectly until here, it do its purpose, but since the while reduces the value of 'a' to the first capital letter it only works once, so here is the reason why I made two variables at the beginning, it adds 1 to stringsize (since the string grew one house) and gives the variable 'a' the new value to keep the for going. However, when I add this two lines of code the error Segmentation fault (core dumped) appears
stringsize++;
a=stringsize;
}
}
}
int main()
{
char s[TAM];
//TAM is equal to 500 btw, I made a define just didn't put it here
scanf("%199[^\n]s", s);
function(s);
printf("%s", s);
return 0;
}
希望我说的够清楚,我不是以英语为母语的人,我是一个编程的初学者
1条答案
按热度按时间bzzcjhmw1#
问题是当它插入一个空格时,它不会跳过它刚找到的大写字母,而是不断地移动它并插入空格。你可以通过在插入空格后递增
i
来解决这个问题: