我的功能在特定点停止

时间:2020-06-01 01:10:47

标签: c

void changeSubstringCase(char str[], char a[]) {
/**
* If `a` is a substring of `str`, then change the case (capitalization) of
* the corresponding substring in `str`. Special characters (' ', '.', ...)
* do not need to be handled. You should only change the letters.
*
* For example:
*
*    char str[100] = "This IS a SENTence WITH BAd CAPITalizaTION";
*    changeSubstringCase(str, "IS a SENTence WI");
*    printf("%s\n", str);
*   
* should print out "This is A sentENCE wiTH BAd CAPITalizaTION".
*/
int slen = 0;
int alen = 0;
int scount = 0;
int counter = 0;
char alpha[1024] = "abcdefghijklmnopqrstuvwxyz";
char Calpha[1024] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
slen = strlen(str);

alen = strlen(a);
for(int i = 0; i < slen; i++)
{
   //printf("a\n");
   if(a[counter] == str[i])
   {
       counter++;
       if(counter == alen)
       {
           //printf("l\n");
            scount = i - counter;
            break;
       }
   }
   else
   {
       counter = 0;
   }
}
printf("Bad boy\n");    
if(counter != alen)
{
    printf("bye\n");
    return;
}
int m = 0;
int f = 0;
while(a[m] != '\0')
{
    for(int i = 0 ; i < 26; i++)
    {
        if(a[m] == alpha[i])
        {
            printf("a\n");
            a[m] = Calpha[i];
            break;
        }
        else if(a[m] == Calpha[i])
        {
            printf("b\n");
            a[m] = alpha[i];
            break;
        }
    }
    m++;
}
for(int i = 0; i < alen; i++)
{
   printf("hel");
   str[scount] = a[i];
   scount++;
}

return;  // Not returning anything. `str` is modified directly.
}
int main() {
char str[MAX_STRING_LEN] = "CSCA48 Exercise 1";

// This should replace the substring:
replaceSubstring(str, "A48 Ex", "A48 sUMMER 2020 Ex");

// This should NOT replace anything, since some of the letters
// in `a` have a different capitalization than in the string, so it is
// NOT a valid substring.
replaceSubstring(str, "summer 2020", "Winter 2020");

// This should change the capitalization of the substring:
changeSubstringCase(str, "sUMMER 2020");

// This should NOT change the capitalization, since the corresponding letters
// in `a` are lowercase, and it is not a valid substring.
changeSubstringCase(str, "csca48");

printf("Expected result: \"CSCA48 sUMMER 2020 Exercise 1\"\n");
printf("Your solution: \"%s\"\n", str);
return 0;
}

由于某种原因,代码将int嵌套在for循环条件语句中。我已经尽力进行了测试,但是我看不出问题为何会在此之后停止。尤其是因为我正在做的所有事情都是试图用大写字母部分代替字母。

任何帮助将不胜感激。

我是编程新手,所以任何复杂的事情都会浪费在我身上

1 个答案:

答案 0 :(得分:0)

您从代码中返回是因为在第一次循环counter != alen之后,您打印了“再见”并返回。

我不确定您要做什么,但这就是为什么它没有明显原因“停止”的原因。

PS-我是使用调试器(在这种情况下为gdb)发现的

相关问题