【问题标题】:C programming for changing all capital letters and changing all lower letters用于更改所有大写字母和更改所有小写字母的 C 编程
【发布时间】:2015-07-08 07:04:17
【问题描述】:

这是我的代码

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

char chaCap(char n);

int main()
{
  char ch,a;
  printf("Enter Sentence\n");

  while((ch=getchar()) != '\n')
  {
    chaCap(ch);
    printf("%c",a);
  }
  printf(" \n");

  return 0;
}
void chaCap(char n)
{
  if(n >= 'a' && n <='z')
      n-=32;

}

我需要

  1. 要求用户输入句子
  2. 将句子全部转换为大写字母并打印
  3. 将句子全部转换为小写字母并打印
  4. 将每个字符从大到小转换,反之亦然。

我做了改变大写字母,但我不能降低,反之亦然。

当我为 2. 和 3 编写代码时,getchar() 什么都不是……

【问题讨论】:

  • ch = toupper(ch); printf("%c", ch);
  • 提示:类似于您通过减去 32 使大写变小的方式,您还应该在字母为大写时添加一些内容以使其更小。
  • 我建议获取字符的 ascii 值,然后添加逻辑:- 如果 ascii 值在 65-97 之间,则 +32 否则如果 97-122 则 -32。就是这样。尝试这样做...
  • 你的程序没有给你任何错误。

标签: c


【解决方案1】:

使用 tolower() 函数:

  while((ch=getchar()) != '\n')
  {
    printf("%c", tolower(ch));
  }

【讨论】:

    【解决方案2】:

    你需要在函数chaCap(char n)中返回值,

    char chaCap(char n)//need to return char instead of void
    {
      if(n >= 'a' && n <='z')
          n-=32;
      return n; // return capital letter here
    }
    

    然后在main()调用时得到返回值,

    int main()
    {
      //... 
      while((ch=getchar()) != '\n')
      {
        a = chaCap(ch); // get return value
        printf("%c",a);
      }
    
    }
    

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      #include <stdlib.h>
      #include <ctype.h>
      
      char *strlwr(char *str){
          char *s;
          for(s = str; *s; ++s){
              if(isupper(*s))
                  *s = tolower(*s);
          }
          return str;
      }
      
      char *strupr(char *str){
          char *s;
          for(s = str; *s; ++s){
              if(islower(*s))
                  *s = toupper(*s);
          }
          return str;
      }
      
      char *strvrs(char *str){
          char *s;
          for(s = str; *s; ++s){
              if(islower(*s))
                  *s = toupper(*s);
              else if(isupper(*s))
                  *s = tolower(*s);
          }
          return str;
      }
      
      int main(void){
          int ch;
          size_t i = 0, size = 32;
          char *str = malloc(size);
      
          printf("Enter Sentence\n");
      
          while((ch=getchar()) != '\n' && ch != EOF){
              str[i++] = ch;
              str[i++] = '\0';
              if(i-- == size){
                  char *temp = realloc(str, (size += 32));
                  if(!temp){
                      fprintf(stderr, "realloc error\n");
                      free(str);
                      exit(EXIT_FAILURE);
                  }
                  str = temp;
              }
          }
          printf("original   : %s\n", str);
          printf("vice versa : %s\n", strvrs(str));
          printf("upper      : %s\n", strupr(str));
          printf("lower      : %s\n", strlwr(str));
          free(str);
      
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        ch 声明为全局变量。

        以下程序会将所有大写字母变为小写,并将所有小写字母变为大写。

        您的程序需要少量修改即可获得所需的结果。我已进行了所需的更改。

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <ctype.h>
        
        void chaCap(char ch);
        char ch;
        
        int main()
        {
        
          printf("Enter Sentence\n");
        
          while((ch=getchar()) != '\n')
          {
           chaCap(ch);
            printf("%c",ch);
          }
          printf(" \n");
        
          return 0;
        }
        void chaCap(char ch)
        {
          if(ch >= 'a' && ch <='z')
             ::ch=ch-32;
          else if(ch>='A'&&ch<='Z')
             ::ch=ch+32;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-26
          • 2022-01-25
          • 1970-01-01
          • 1970-01-01
          • 2011-06-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多