字符串中大小写字母转换小程序
生活随笔
收集整理的這篇文章主要介紹了
字符串中大小写字母转换小程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、大寫轉小寫
參數:char指針或者char數組
功能:如果傳入參數包含大寫字母,將其轉換成小寫字母,其他字符保持不便;
1 #include <stdio.h>
2 #include <string.h>
3
4 char* CapToLow(char *data)
5 {
6 int i=0;
7 if(data==NULL)
8 return;
9
10 for(i = 0; i < strlen(data); i++)
11 {
12 if( (data[i] >= 'A') && (data[i] <= 'Z') )
13 {
14 data[i] = tolower(data[i]);
15 }
16 }
17
18 return;
19 }
二、小寫轉大寫
參數:char指針或者char數組
功能:如果傳入參數包含小寫字母,將其轉換成大寫字母,其他字符保持不便;
1 #include <stdio.h>
2 #include <string.h>
3
4 char* LowToCap(char *data)
5 {
6 int i=0;
7 if(data==NULL)
8 return;
9
10 for(i = 0; i < strlen(data); i++)
11 {
12 if( (data[i] >= 'a') && (data[i] <= 'z') )
13 {
14 data[i] = toupper(data[i]);
15 }
16 }
17
18 return;
19 }
三、互轉
參數:char指針或者char數組
功能:把傳入參數包含的小寫字母轉換成大寫字母,大寫字母轉換成小寫字母,其他字符保持不便;
1 #include <stdio.h>
2 #include <string.h>
3
4 char* ConvertStr(char *data)
5 {
6 int i=0;
7 if(data==NULL)
8 return;
9
10 for(i = 0; i < strlen(data); i++)
11 {
12 if( (data[i] >= 'A') && (data[i] <= 'Z') )
13 {
14 data[i] = tolower(data[i]);
15 }
16 else if( (data[i] >= 'a') && (data[i] <= 'z') )
17 {
18 data[i] = toupper(data[i]);
19 }
20 }
21
22 return;
23 }
四、tolower 和 toupper 函數原型
1 int tolower(int c)
2 {
3 if ((c >= 'A') && (c <= 'Z'))
4 return c + ('a' - 'A');
5 return c;
6 }
7
8 int toupper(int c)
9 {
10 if ((c >= 'a') && (c <= 'z'))
11 return c + ('A' - 'a');
12 return c;
13 }
總結
以上是生活随笔為你收集整理的字符串中大小写字母转换小程序的全部內容,希望文章能夠幫你解決所遇到的問題。