这种题目相对简单,用C语言实现很好理解。
,
//用C语言实现:判断一个程序是否为小写字母,如果是,就转换成大写字母,否则,不转换
#include
using namespace std;
int main(void) {
char c;
cout << "请输入一个字符: " << endl;
cin >> c;
if (c >= 'a' && c <= 'z') { //判断是否为小写字母,。,,,利用ASCLL码表
c = c - 32; //转换成大写
}
else if (c >= 'A' && c <= 'Z') { //判断是否为大写字母
c = c + 32; //转换成小写
}
cout << c << endl;
system("pause");
return 0;
}
用go语言实现的两种方法
package main
import(
"fmt"
)
func main(){
var x string
fmt.Println("请输入字符:")
fmt.Scanln(&x)
t:= snfn(x)
fmt.Println(t)
}
func snfn(x string)string{
c := rune(x[0])
c -= 32
return string(c)
}
package main
import "fmt"
func main(){
for {
var x string
fmt.Println("请输入一个字符:")
fmt.Scanln(&x)
t := converSion(x)
fmt.Println(t)
}
}
func converSion(a string) string {
var ay = []string{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}
var ay2 = []string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
for k, v := range ay{
if v == a{
a = ay2[k]
}
}
return a
}
//还可以使用strings包中的
//strings.ToLower()
//strings.ToTitle()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)