Home  >  Article  >  Backend Development  >  Convert string to uppercase letters in C

Convert string to uppercase letters in C

PHPz
PHPzforward
2023-08-30 21:01:111357browse

Convert string to uppercase letters in C

这是一个将字符串转换为大写字母的C语言程序示例:

示例

#include <stdio.h>
#include <string.h>
int main() {
   char s[100];
   int i;
   printf("</p><p>Enter a string : ");
   gets(s);
   for (i = 0; s[i]!=&#39;\0&#39;; i++) {
      if(s[i] >= &#39;a&#39; && s[i] <= &#39;z&#39;) {
         s[i] = s[i] -32;
      }
   }
   printf("</p><p>String in Upper Case = %s", s);
   return 0;
}

输出

Enter a string : hello world!
String in Upper Case = HELLO WORLD!

在程序中,字符串转换为大写的实际代码位于main()函数中。声明一个 char 类型的数组 s[100],该数组将存储用户输入的字符串。

然后,使用 for 循环将字符串转换为大写字符串,并使用 if 块检查 if字符为小写,则通过从其 ASCII 值中减去 32 将其转换为大写。

for (i = 0; s[i]!=&#39;\0&#39;; i++) {
   if(s[i] >= &#39;a&#39; && s[i] <= &#39;z&#39;) {
      s[i] = s[i] -32;
   }
}

The above is the detailed content of Convert string to uppercase letters in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete