【问题标题】:Pointer and Array in CC中的指针和数组
【发布时间】:2013-03-26 22:23:10
【问题描述】:

我是 C 和编程新手。我被困在家庭作业中。我的输出仅显示大写的第一个字符,以及一些奇怪数字的以下字符。有人可以看看我的代码并就我做错了什么以及解决问题的方法给我一些提示吗?非常感谢您的帮助!

"编写一个函数 void sticky(char* word) ,其中 word 是单个单词,例如“sticky”或“RANDOM”。sticky() 应该修改单词以显示为“sticky caps” (http://en.wikipedia.org/wiki/StudlyCaps),也就是说,字母必须是大小写交替的(大写和小写),第一个字母从大写开始。例如,“sticky”变成“StIcKy”,“RANDOM”变成“RaNdOm”。注意结尾字符串,用 '\0' 表示。你可以假设给 sticky() 函数提供了合法的字符串。"

#include <stdio.h>
#include <stdlib.h>

/*converts ch to upper case, assuming it is in lower case currently*/
char toUpperCase(char ch)
{
 return ch-'a'+'A';
}

/*converts ch to lower case, assuming it is in upper case currently*/
char toLowerCase(char ch)
{
 return ch-'A'+'a';
}

void sticky(char* word){
 /*Convert to sticky caps*/

for (int i = 0; i < sizeof(word); i++)
{
    if (i % 2 == 0)
    {
        word[i] = toUpperCase(word[i]);
    }
    else
    {
        word[i] = toLowerCase(word[i]);
    }
}

}

int main(){
/*Read word from the keyboard using scanf*/
char word[256];
char *input;
input = word;
printf("Please enter a word:\n");
scanf("%s", input);

/*Call sticky*/
sticky(input);

/*Print the new word*/
printf("%s", input);

for (int i = 0; i < sizeof(input); i++)
{
    if (input[i] == '\n')
    {
        input[i] = '\0';
        break;
    }
}

return 0;

}

【问题讨论】:

  • /*converts ch to lower case, assuming it is in upper case currently*/ 反之亦然是个大线索。
  • 融合 Keith 的答案和 ritesh 的似乎最好

标签: c arrays pointers


【解决方案1】:

您需要使用 strlen 而不是 sizeof 来查找 char* 字符串的长度

【讨论】:

    【解决方案2】:

    修改您的change upperchange lower 函数

    /*converts ch to upper case,*/
    char toUpperCase(char ch)
    {
        if(ch>='a' && ch<='z')/*If condition just to make sure current letter is in  lower case*/
            return ch-'a'+'A';
    }
    
    /*converts ch to lower case, assuming it is in upper case currently*/
    char toLowerCase(char ch)
    {
        if(ch>='A' && ch<='Z')/*If condition just to make sure current letter is in  Upper case*/
            return ch-'A'+'a';
    }
    

    此外,只有four characters 被转换,因为您使用sizeof 来查找字符串长度。sizeof 始终返回 4(取决于机器)。 use strlen(word) 在下面的 for 循环中查找字符串 word 的长度:

    for (int i = 0; i < strlen(word); i++)
    {
    }
    

    【讨论】:

      【解决方案3】:

      您应该使用strlen 而不是sizeof。 另外,你必须检查你的字母是大写还是小写:

      for (int i = 0; i < strlen(word); i++)
      {
          if (i % 2 == 0)
          {
              if ( isLowerCase(word[i]) )
              {
                  word[i] = toUpperCase(word[i]);
              }
              else
              {
                  // do nothing.
              }
          }
          else
          {
              if ( isUpperCase(word[i]) )
              {
                  word[i] = toLowerCase(word[i]);
              }
              else
              {
                  // do nothing.
              }
          }
      }
      

      请注意,我还没有实现isUpperCaseisLowerCase 函数;D

      【讨论】:

        【解决方案4】:

        函数sizeof()用于计算数据类型的大小,而不是分配给指针的大小。

        所以你不能像sizeof(word)那样使用它。相反,遍历一个字符,直到你偶然发现一个 \0,它表示字符串的结尾。

        举例:

        int i = 0;
        while ( word[i] != 0 ) {
          // do lower/upper case conversion.
        }
        

        【讨论】:

          【解决方案5】:

          sizeof (word)是一个char *的大小,你必须用数组大小​​传递另一个参数...或者使用strlen()。

          【讨论】:

            【解决方案6】:

            您的代码有问题:您将奇数字符设为大写,将偶数设为小写,但您一开始没有检查它们是小写还是大写。但是降低一个已经小写的字母会给你一个错误的值(“大写”一个已经大写的字母也是如此)。

            所以你应该这样做:

            char toUpperCase(char ch)
            {
             if ((ch >= 'a') && (ch <= 'z')) {
               return ch-'a'+'A';
             } else {
               return ch;
             }
            }
            

            toLowerCase 也是如此。

            【讨论】:

              【解决方案7】:

              非常感谢您的提示!根据您的建议,我修改了我的代码,它现在可以工作了。

              以下是我修改后的代码:

              #include <stdio.h>
              #include <stdlib.h>
              #include <string.h>
              
              /*converts ch to upper case, assuming it is in lower case currently*/
              char toUpperCase(char ch){
                  return ch-'a'+'A';
              }
              
              /*converts ch to lower case, assuming it is in upper case currently*/
              char toLowerCase(char ch){
                  return ch-'A'+'a';
              }
              
              
              
              void sticky(char* word)
              {
               /*Convert to sticky caps*/
              
              for (int i = 0; i < strlen(word); i++)
              {
                  if (i % 2 == 0)
                  {
                      if (word[i] >= 'a' && word[i] <= 'z')
                      {
                          word[i] = toUpperCase(word[i]);
                      }
              
                  }
                  else
                  {
                      if (word[i] >= 'A' && word[i] <= 'Z')
                      {
                          word[i] = toLowerCase(word[i]);
                      }
              
                  }
              }
              

              }

              int main(){
              /*Read word from the keyboard using scanf*/
              char word[256];
              char *input;
              input = word;
              printf("Please enter a word:\n");
              scanf("%s", input);
              
              /*Call sticky*/
              sticky(input);
              
              /*Print the new word*/
              printf("%s", input);
              
              for (int i = 0; i < sizeof(input); i++)
              {
                  if (input[i] == '\n')
                  {
                      input[i] = '\0';
                      break;
                  }
              }
              
              return 0;
              

              }

              【讨论】:

                猜你喜欢
                • 2023-03-11
                • 2013-03-10
                • 1970-01-01
                • 1970-01-01
                • 2015-03-27
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多