我試圖解決一個關于如何將大寫字母更改為其下一個字母并保持小寫字母不變的問題。起初我試圖檢查字串中是否存在任何大寫字母,然后我應該嘗試將其更改為小寫字母。但我認為我的代碼有問題。這是我的一段代碼:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char alpha[100];
printf("Enter String: ");
gets(alpha);
for (int i = 0; alpha[i] != 0; i )
{
if (alpha[i] >= 'A' && alpha[i] <= 'Z')
{
//upperL ;
if (alpha[i] >= 65 && alpha[i] <= 90)
{
printf("\n %c", alpha 1);
}
else if (alpha[i] >= 97 && alpha[i] <= 122)
{
printf("\n %c", alpha 1);
}
else if (alpha[i] == 90)
{
printf("\n %c", 65);
}
else if (alpha[i] == 122)
{
printf("\n %c", 122);
}
else
{
printf("\n %c", alpha);
}
}
}
getch();
return 0;
}
uj5u.com熱心網友回復:
您的大寫測驗適用于 ASCII 編碼,但您更改字符的大小寫不正確:printf("\n %c", alpha 1);傳遞指向陣列中第二個字符的指標。你應該寫:
printf("\n %c", alpha[i] 1);
您還應該處理非大寫字符,這些字符會被忽略,因為else初始測驗中沒有子句。
您需要特殊情況,'Z'但您的測驗失敗,因為測驗'Z'已經處理了(alpha[i] >= 65 && alpha[i] <= 90)。使用硬編碼的 ASCII 值很難閱讀且不可移植。
其他測驗總是失敗,因為它們嵌套在測驗第一個大寫字符的測驗中。
另請注意,您不應使用gets(),它不能安全使用并且已從 C 標準的最新版本中洗掉。使用fgets()來代替。
這是一個修改后的版本:
#include <stdio.h>
int main() {
char alpha[100];
printf("Enter String: ");
if (fgets(alpha, sizeof alpha, stdin)) {
for (int i = 0; alpha[i] != '\0'; i ) {
if (alpha[i] >= 'A' && alpha[i] < 'Z') {
alpha[i] = 1;
} else
if (alpha[i] == 'Z') {
alpha[i] = 'A';
}
}
fputs(alpha, stdout);
}
getchar();
return 0;
}
上面的代碼假設大寫字母被編碼為一個連續的塊。這就是今天幾乎無處不在的 ASCII 的情況。下面是一個可以處理其他字符集的便攜版本:
#include <stdio.h>
#include <string.h>
int main() {
char alpha[100];
const char *upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZA";
printf("Enter String: ");
if (fgets(alpha, sizeof alpha, stdin)) {
for (int i = 0; alpha[i] != '\0'; i ) {
const char *p = strchr(upper, alpha[i]);
if (p != NULL) {
alpha[i] = p[1];
}
}
fputs(alpha, stdout);
}
getchar();
return 0;
}
uj5u.com熱心網友回復:
您應該將功能放在一個函式中,從而給它一個名稱,然后它將分配與環境的其余部分隔離開來。
以這個為例:
#include <assert.h>
#include <ctype.h>
void nextUpperCase(char *str) {
assert(str && "function `nextUpperCase' not designed to handle null pointers");
for (; *str != '\0'; str)
if (isupper(*str) && !isalpha( *str))
*str = 'A';
}
然后無需手動輸入即可輕松測驗:
// #include <locale.h> Needed for locale other than "C"
#include <stdio.h>
int main() {
// setlocale(LC_ALL, "C"); // this is implicitly done before main is entered
char buf[] = "Hello, Zee World!";
puts(buf); // prints `Hello, Z World!'
nextUpperCase(buf);
puts(buf); // prints `Iello, A Xorld!'
nextUpperCase(NULL); // assertion
return 0;
}
這個特定的解決方案假定“C”語言環境。如果使用任何其他語言環境,則此建議的“下一步”解決方案假定 'A'...'Z' 沒有孔。對于其他語言環境,“下一個”定義將需要非常不同的東西。
uj5u.com熱心網友回復:
試試這個代碼:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char alpha[100];
printf("Enter String: ");
gets(alpha);
for (int i = 0; alpha[i] != 0; i )
{
if (alpha[i] >= 'A' && alpha[i] <= 'Z')
{
//upperL ;
if (alpha[i] >= 65 && alpha[i] <= 90)
{
printf("\n %c", alpha 1);
}
else if (alpha[i] >= 97 && alpha[i] <= 122)
{
printf("\n %c", alpha 1);
}
else if (alpha[i] == 90)
{
printf("\n %c", 65);
}
else if (alpha[i] == 122)
{
printf("\n %c", 122);
}
else
{
printf("\n %c", alpha);
}
}
}
getch();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/399354.html
