简体   繁体   English

C 程序将小写转换为大写以用于标准输出的小问题

[英]Small Issue with C program to convert lowercase to uppercase for stdout

I am learning about system calls in linux and I wrote a quick program to copy stdin to stdout.我正在学习 linux 中的系统调用,我编写了一个快速程序来将标准输入复制到标准输出。

I want to convert the stdin lowercase letters to uppercase letters before they are written.我想在它们被写入之前将标准输入小写字母转换为大写字母。

I implemented a function with a pointer to the array to capitalize the characters.我用指向数组的指针实现了一个 function 以大写字符。 It only capitalizes the first letter of the write though and I don't understand why.它只大写写的第一个字母,我不明白为什么。 From what I understand, I wouldn't need a for loop because of the read() system call.据我了解,由于 read() 系统调用,我不需要 for 循环。

#include <unistd.h>
#define SIZE 512
char charToUpper(char *);

int main () {
    int nread;
    char buf[SIZE];
    while (nread = read(0, buf , SIZE)) {
        charToUpper(buf);
        write(1,buf,nread);
    }
    return 0;
}

char charToUpper(char *a){
    if ((*a > 96) && (*a <123)) {
        *a = *a-32;
        return *a;
    }
}

Your charToUpper receives a pointer to a char , and you sent it buf , which decays to a pointer to a char of the first character in buf , hence your result.您的charToUpper收到一个指向char的指针,然后您将其发送到buf ,该指针衰减为指向buf中第一个字符的char的指针,因此是您的结果。

Remember in c you do not get the size of the array you are passing for free - you have to pass it on as well.请记住,在c中,您不会免费获得要传递的数组的大小 - 您也必须传递它。 Consider all your operations in charToUpper are on *a , which is of type char , a single character.考虑您在charToUpper中的所有操作都在*a上,它是char类型,一个单个字符。 So to fix this change the declaration to因此,要解决此问题,请将声明更改为

char charToUpper(char *, unsigned int size);

so know you know how many characters you actually read and need to change.所以知道你知道你实际阅读了多少个字符并且需要更改。 Try and change your program now to suit this.现在尝试更改您的程序以适应这种情况。 One hint - your return will probably have to move for example.一个提示 - 例如,您的return可能必须移动。

he program is perfectly fine except for the loop part.除了循环部分外,他的程序非常好。

char* charToUpper(char *a){
    char *t=p;
    while(*a!='\0'){
    if ((*a > 96) && (*a <123)) 
        *a = *a-32;
    a++;
    }
    return t;
}

you didn't incremented the loop.你没有增加循环。 do it and u will get去做,你会得到

The following proposed code:以下建议的代码:

  1. cleanly compiles干净地编译
  2. properly terminates the input array of characters正确终止输入的字符数组
  3. properly changes all lower case characters to upper case, using the facility: toupper() from the header file: ctype.h使用工具将所有小写字符正确更改为大写: toupper()来自 header 文件: ctype.h
  4. performs the desired functionality执行所需的功能

and now, the proposed code:现在,建议的代码:

#include <unistd.h>
#include <ctype.h>   // toupper()

#define SIZE 512

void charToUpper(char *);

int main ( void ) 
{
    ssize_t nread;
    char buf[SIZE];
    while ( (nread = read(0, buf , SIZE) ) > 0) 
    {
        // NUL terminate the character string
        buf[nread] = '\0';

        // convert all characters to uppercase
        charToUpper(buf);

        // output the (all uppercase) string to stdout
        write(1,buf, (size_t)nread);
    }
    return 0;
}


void charToUpper( char buf[] )
{
    for( size_t i = 0; buf[i]; i++ )
    {
        buf[i] = (char)toupper( buf[i] );
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM