2016-02-03 45 views
1

我試圖在c中將char *轉換爲大寫字母,但功能toupper()在此處不起作用。將字符*轉換爲大寫字母C

我試圖得到temp的值的名稱,名稱是冒號前的任何東西,在這種情況下它是「測試」,然後我想充分利用名稱。

void func(char * temp) { 
// where temp is a char * containing the string "Test:Case1" 
char * name; 

name = strtok(temp,":"); 

//convert it to uppercase 

name = toupper(name); //error here 

} 

我得到的錯誤,函數toupper期望一個int,但收到一個char *。事情是,我必須使用char *的函數,因爲這是函數所採用的,(我不能在這裏真正使用char數組,我能嗎?)。

任何幫助將不勝感激。

回答

7

toupper()轉換單個char

只需使用一個循環:

void func(char * temp) { 
    char * name; 
    name = strtok(temp,":"); 

    // Convert to upper case 
    char *s = name; 
    while (*s) { 
    *s = toupper((unsigned char) *s); 
    s++; 
    } 

} 

細節:標準庫函數toupper(int)所有unsigned charEOF定義。由於char可能會被簽名,請轉換爲unsigned char

一些操作系統的支持函數調用,這是否:upstr()strupr()

1

toupper()作品一個元素(int參數,取值範圍EOF一樣的unsigned char或)在時間上。

原型:

int toupper(int c);

你需要使用一個循環在時間從字符串提供一個元素。

+0

也許我會刪除這個答案,因爲它不會增加價值,但是DV的原因是什麼? –

1

toupper()僅適用於單個字符。但有strupr()這是你想要的一個指向字符串的指針。

+2

'strupr'不是標準的。據我所知,它只支持微軟的圖書館。 – interjay

+0

@interjay:Greenhills和Borland也支持它。但你是對的,它不是glibc。 – wallyk

相關問題