2013-10-29 65 views
0

不管大小写如何比较字符。意思是假设我输入Z,如果我的条件应该对Z和Z都返回true,则字符可以是来自A-Z或a-z的任何字母。
Like ABCbabcAABC
如果我输入B,那么它必须得到4作为输出,因为在字符串中有4个B。
我在Turbo C++上学习C++。我正在尝试做,但现在不能解决问题。在C++中将字符转换为大写和小写

void main() 
{ 
    clrscr(); 

    char str[50],ch; 
    char str1[50]; 

    int i = 0, l; 

    cout << "Enter strings: "; 
    gets(str); 
    cout << "Enter charcter: "; 
    cin >> ch; 

    l = strlen(str); 

    for(i = 0; i <= l; i++) 
    { 
     cout << isupper(str[i]) ? tolower(str[i]) : toupper(str[i]); 
    } 

    puts(str); 
    getch(); 
} 
+0

但是这个代码将切换大/小写! AaBB将成为一种乐趣! –

+0

小心,你的是错的。 <=你正在阅读字符串外部的东西,在这种情况下,'\ 0' –

+0

@ Lamp-up-duck我正在尝试做同样的事情来猜测切换到较低和较高的情况,但如果我进入教会和字符切换'C'然后我出去400000Church。我应该输出为churCh。为什么我没有得到正确的输出指导 – user2747954

回答

0
void main() 
{ 
    clrscr(); 
    char str[50],ch; 
    char str1[50]; 
    int i=0,l; 
    cout<<"Enter strings: "; 
    gets(str); 
    cout<<"Enter charcter: "; 
    cin>>ch; 
    l=strlen(str); 
    int result=0; 
    for(i=0;i<l;i++) 
    { 
      if(tolower(ch)== tolower(str[i])) 
     { 
      result++; 
     } 
    } 
puts(str); 
puts(result); 
getch(); 
} 
+0

我遵循你的技巧并将此代码放在循环中if(toupper(ch)== str [i]) str [i] = tolower的(STR [1]); cout <<“LOWER”<< str [i] << endl; (tolower(ch)== str [i]) str [i] = toupper(str [i]); cout <<“UPPER”<< str [i] << endl; }'但我得到输出AAAA的str [] = AAAA它应该是aaaa – user2747954

+0

正确地告诉你需要什么?你需要数字的字符?或者你需要打印字符? – sam

0
if (tolower(str[i]) == tolower(ch)) { 
    cout << (isupper(str[i]) ? tolower(str[i]) : toupper(str[i])); 
} else { 
    cout << str[i]; 
} 

40000是isupper输出。非零的upcase字符,零小写字母,写在manual

0

所有你需要做的就是用ASCII值周围玩耍。

检出代码:这说明您只需要转换那些ASCII值在65(A)到90(Z)之间的字符。

公共类Ques2 {

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException { 
    //ascii A=65 Z=90 a=97 
    System.out.println("Enter UPPERCASE"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    // String input = br.readLine().toLowerCase(); 
    char c; 
    char[] word=br.readLine().toCharArray(); 
    for(int i :word) 
    { 
     if(i>=65 && i<=90){ 
     i=i+32 ; 
     c=(char) i; 
     System.out.println(c); 
     } 
     else{ 
      c=(char)i; 
      System.out.println(c); 
     } 
    } 

} 

}

相关问题