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

Convert string to uppercase letters in C

PHPz
Release: 2023-08-30 21:01:11
forward
1533 people have browsed it

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;
}
Copy after login

输出

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

在程序中,字符串转换为大写的实际代码位于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;
   }
}
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template