返回列表 發帖

403 字串與檔案處理 (字串大小寫轉換)

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讓使用者輸入英文字串(無空白字元),字串長度不超過100字元,將字串中小寫字母轉成大寫字母、大寫字母轉成小寫字母後輸出。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
英文字串(無空白字元)

輸出說明
此字串大小寫轉換

範例輸入
ABcdefGH
範例輸出
abCDEFgh

本帖隱藏的內容需要回復才可以瀏覽

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string str;
  6.     cin >> str;
  7.     for(int i=0 ; i <str.size() ; i++)
  8.     {
  9.         if(str[i] >= 'A' && str[i] <= 'Z')
  10.         {
  11.             cout << char(str[i] - 'A' + 'a');
  12.         }
  13.         else
  14.         {
  15.             cout << char(str[i] = str[i] - 'a' + 'A');
  16.         }
  17.     }

  18.     return 0;
  19. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string n;
  6.     cin>>n;
  7.     for(int i=0;i<n.length();i++)
  8.     {
  9.         if(n[i]>='A'&&n[i]<='Z')

  10.             cout<<char(n[i]-'A'+'a');
  11.         else
  12.             cout<<char(n[i]-'a'+'A');
  13.     }

  14. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string str;
  6.     cin>>str;
  7.     for(int i=0 ; i<str.length() ; i++)
  8.     {
  9.         if(str[i]>= 'A' && str[i]<= 'Z')
  10.             str[i]=str[i]-'A'+'a';
  11.         else
  12.             str[i]=str[i]-'a'+'A';
  13.     }
  14.     cout<<str<<endl;
  15. }
複製代碼
Vincent

TOP

返回列表