无需使用 C 标准库中可用的其他函数即可将小写字符数组转换为大写字符数组的 C 程序
C Program to convert lowercase to uppercase char array without using other functions available in the C standard library
我正在尝试将所有小写字母替换为大写字母,而不使用 C 标准库中可用的其他函数和指针。
我有我的 main.c:
#include <stdio.h>
#include "upper1.h"
int main() {
char string[] = "HelloWorld";
int arraySize = sizeof(string) / sizeof(string[0]);
printf("String before transformation: ");
int i;
for (i= 0; i< arraySize; i++) {
printf("%c", string[i]); //prints array information
}
printf("\n");
char *str = string; //pointer that points to the array
upper1(str);
printf("String before transformation: ");
int j;
for (j= 0; j< arraySize; j++) {
printf("%c", string[i]); //prints array information
}
printf("\n");
return 0;
}
我有我的函数代码文件:
void upper1(char *str) {
int i;
for(i = 0; i < 10; i++) {
if(*str >= 65 + 32 && *str <= 90 + 32) { //65+32 is "a" and 90+32 is "z"
*str = *str - 32;
}
str++;// skips one position of the array
}
}
出于某种原因,当我 运行 我得到:
gcc -g -Wall -c upper1.c -o upper1.o
gcc -g -Wall -c main.c -o main.o
gcc upper1.o main.o -o ex04
./ex04
转换前的字符串:HelloWorld
转换前的字符串:������������
而不是
gcc -g -Wall -c upper1.c -o upper1.o
gcc -g -Wall -c main.c -o main.o
gcc upper1.o main.o -o ex04
./ex04
转换前的字符串:HelloWorld
转换前的字符串:HELLOWORLD
(我有一个文件“upper1.h”,但只有在 void upper1(char *str);
时才是正确的)
这是因为您在第二个循环中使用 i
而不是 j
。
printf("String before transformation: ");
int j;
for (j = 0; j < arraySize; j++) {
printf("%c", string[j]); // use 'j' instead of 'i'
}
您可以通过在 for-loop 中声明您的循环计数器来避免将来出现这样的拼写错误。这确保计数器只能在循环范围内访问,不能在另一个循环中重用:
printf("String before transformation: ");
for (int j = 0; j < arraySize; j++) {
printf("%c", string[j]);
}
我正在尝试将所有小写字母替换为大写字母,而不使用 C 标准库中可用的其他函数和指针。 我有我的 main.c:
#include <stdio.h>
#include "upper1.h"
int main() {
char string[] = "HelloWorld";
int arraySize = sizeof(string) / sizeof(string[0]);
printf("String before transformation: ");
int i;
for (i= 0; i< arraySize; i++) {
printf("%c", string[i]); //prints array information
}
printf("\n");
char *str = string; //pointer that points to the array
upper1(str);
printf("String before transformation: ");
int j;
for (j= 0; j< arraySize; j++) {
printf("%c", string[i]); //prints array information
}
printf("\n");
return 0;
}
我有我的函数代码文件:
void upper1(char *str) {
int i;
for(i = 0; i < 10; i++) {
if(*str >= 65 + 32 && *str <= 90 + 32) { //65+32 is "a" and 90+32 is "z"
*str = *str - 32;
}
str++;// skips one position of the array
}
}
出于某种原因,当我 运行 我得到:
gcc -g -Wall -c upper1.c -o upper1.o gcc -g -Wall -c main.c -o main.o gcc upper1.o main.o -o ex04 ./ex04 转换前的字符串:HelloWorld 转换前的字符串:������������
而不是
gcc -g -Wall -c upper1.c -o upper1.o gcc -g -Wall -c main.c -o main.o gcc upper1.o main.o -o ex04 ./ex04 转换前的字符串:HelloWorld 转换前的字符串:HELLOWORLD
(我有一个文件“upper1.h”,但只有在 void upper1(char *str);
时才是正确的)
这是因为您在第二个循环中使用 i
而不是 j
。
printf("String before transformation: ");
int j;
for (j = 0; j < arraySize; j++) {
printf("%c", string[j]); // use 'j' instead of 'i'
}
您可以通过在 for-loop 中声明您的循环计数器来避免将来出现这样的拼写错误。这确保计数器只能在循环范围内访问,不能在另一个循环中重用:
printf("String before transformation: ");
for (int j = 0; j < arraySize; j++) {
printf("%c", string[j]);
}