1. 程式人生 > >C語言字串轉換大寫

C語言字串轉換大寫

要求:比如1gjfg45 轉換成:1GIFG45

#include <stdio.h>

int main() {

	char str[100] = "nishiw34odeyan4343";
	char tar[100];
	int index = 0;
	int pos = 0;
	//轉換為大寫
	while(str[index] != '\0') {
	
		str[index] = (str[index]  >= 'a' && index <= 'Z') ? (char)(str[index] - 'a' + 'A'): str[index];	
		index ++;
	}

	puts(str);
	//陣列逆序 
	while(index != 0) {
		tar[pos] = str[--index];
		pos ++;
	}

	tar[pos] = '\0';
	puts(tar);
	return 0;
}

執行結果:

NISHIW34ODEYAN4343

3434NAYEDO43WIHSIN