简体   繁体   English

我正在写 C function 使用 ASCII 将小写字符转换为大写字符,但 Output 不正确

[英]I am writing C function that convert lowercase char to upper case char with using ASCII but Output is not correct

Okay, So I start working on this, I have code below;好的,所以我开始研究这个,我有下面的代码;

+I also have strlen("any string here") func that return len of any str in decimal just keep in your mind. +我还有 strlen("any string here") 函数,它以十进制返回任何 str 的 len,请记住。

I take a lover case let's say a, then a will be equal some decimal num in ASCII table then I subtract 32 to get A.我以情人的情况为例,假设a,然后a将等于ASCII表中的某个十进制数字,然后我减去32得到A。

Sadly this is not working, any idea for this?可悲的是,这不起作用,对此有什么想法吗? Thank you for all help and your time!感谢您的所有帮助和您的时间!

int uppercase(char sent[]) {
    
 for(int i=0; i <= strlen(sent); ++i) {
        if(sent[i]>='a' && sent[i]<='z')
            sent[i] -= 32;
}

The function is declared as having the return type int but returns nothing. function 被声明为具有返回类型int但不返回任何内容。

int uppercase(char sent[]) {
    
 for(int i=0; i <= strlen(sent); ++i) {
        if(sent[i]>='a' && sent[i]<='z')
            sent[i] -= 32;

}

In general for a function that deals with strings the condition of the for loop should look at least like一般来说,对于处理字符串的 function,for 循环的条件至少应该看起来像

 for(int i=0; i < strlen(sent); ++i) {

Though it is better to write the loop like虽然最好像这样编写循环

 for( size_t i = 0, n = strlen(sent); i < n; ++i ) {

However there is no great sense to use the function strlen in the function uppercase .但是,在 function uppercase中使用 function strlen并没有什么意义。 Its call is redundant.它的调用是多余的。

Pay attention to that you may not change a string literal.请注意,您可能不会更改字符串文字。 Any attempt to change a string literal results in undefined behavior.任何更改字符串文字的尝试都会导致未定义的行为。

From the C Standard (6.4.5 String literals)来自 C 标准(6.4.5 字符串文字)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. 7 不确定这些 arrays 是否不同,只要它们的元素具有适当的值。 If the program attempts to modify such an array, the behavior is undefined.如果程序试图修改这样的数组,则行为未定义。

Also it is better not to use the magic number 32 .另外最好不要使用幻数32

The function can be written the following way as it is shown in the demonstrative program below. function 可以按以下方式编写,如下面的演示程序所示。

#include <stdio.h>

char * uppercase( char *s )
{
    for ( char *p = s; *p; ++p )
    {
        if ( 'a' <= *p && *p <= 'z' ) *p = *p & ~' ';
    }
    
    return s;
}

int main(void) 
{
    char s[] = "hello world!";
    
    puts( s );
    puts( uppercase( s ) );
    
    return 0;
}

The program output is程序 output 是

hello world!
HELLO WORLD!

As for the function strlen then it is better to use another name for the function because it will conflict with the standard C function strlen . As for the function strlen then it is better to use another name for the function because it will conflict with the standard C function strlen . And the function itself can be defined the following way而function本身可以定义如下

size_t string_len( const char *s )
{
    const char *p = s;

    while ( *p ) ++p;

    return p - s;
}

This code can help you这段代码可以帮助你

#include <stdio.h>
#include <string.h>

void uppercase(char T[],int k)
{
      int i=0;
      while(i<k)
      {
           if(T[i]>='a'&&T[i]<='z')
           {
                 T[i]=(char)((int)T[i]-32);
           }
         i++;
      }
      i=0;
      while(i<k)
      {
            printf("%c",T[i]);
            i++;
      }
      printf("\n");
}

int main()
{
      char T[]="good morning !";
      int k=sizeof(T);
      uppercase(T,k);
}
/*
Parsing the string, then making the letters to uppercase.
*/

#include <stdio.h>
#include <limits.h>



int strlen(char s[]){ //String length function
    int i;
    for (i = 0; s[i] != '\0'; i++);
    return i;
}

void uppercase(char sent[]) {
    for(int i=0; i < strlen(sent); ++i) {
        if(sent[i]>='a' && sent[i]<='z')
            sent[i] += 32;
    }

 printf("%s", sent);
}

this is a whole tab of my whole work.这是我整个工作的一个完整标签。 when i try uppercase("hello world");当我尝试大写时(“你好世界”); it giving me core dumped console problem.它给了我核心转储控制台问题。

This one will work:这将起作用:

#include <stdio.h>
#include <string.h>

void uppercase(char sent[]) {
    for (int i = 0; i < (int)strlen(sent); i++) {
        if (sent[i] >= 'a' && sent[i] <= 'z') {
            sent[i] -= 32;
        }
    }
}

int main(int argc, char* argv[]) {
    if (argc > 1){
        uppercase(argv[1]);
        puts(argv[1]);
    }

    return 0;
}

It compiles without any errors and warnings (using clang ), even with options -pedantic -Wall -Wextra .它编译时没有任何错误和警告(使用clang ),即使使用选项-pedantic -Wall -Wextra

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

相关问题 使用C中的函数将大写字符串转换为小写的程序中的问题 - Problem in a program to convert upper case string to lowercase using function in C 尝试在不使用函数的情况下将C中的大写字符转换为小写字母 - Trying to convert uppercase char to lowercase in C without using a function 如何编写一个原型应为“void convertstring(char *)”的 function 以将 C 中的小写转换为大写? - How can I write a function whose prototype should be 'void convertstring(char *)' to convert lower case to upper case in C? 如何在C中将字符串(char *)转换为大写或小写 - How to convert a string (char *) to upper or lower case in C 在C中将ascii char []转换为十六进制char [] - Convert ascii char[] to hexadecimal char[] in C 将char数组元素转换为大写或小写 - convert char array elemnts to upper or lower case 编写Linux内存驱动程序以将小写转换为大写 - Writing a Linux memory driver to convert lowercase to upper case C-如何将char(8位)转换为ASCII格式 - C - how do I convert char(8-bit) to ascii format 将char *字符串转换为大写 - Convert char* string to upper 编写 C 函数以返回一个字符并为 char 变量赋值,但我遇到了一些问题 - Writing C function to return a character and assigning a value to a char variable, but I am having some problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM