简体   繁体   English

C程序将字符串大写不起作用

[英]C program to capitalize string not working

So, for an assignment, I had to finish a code meant to capitalize a string. 因此,对于作业,我必须完成一个用于大写字符串的代码。

What I tried was this: 我试过的是这个:

#include <stdio.h>

void capitalize(char *str)
{
    int i = 0;
    if (str[i] >= 97 && str[i] <= 122)
    {
        str[i] = str[i] - 32;
    }
    else
    {
        i++;
    }
}

void strCopy(char *str2, char *str1)
{
    while (*str2)
    {
      *str1 = *str2;
      str2++;
      str1++;
    }
    *str1 = '\0';
}

int main(int argc, char **argv)
{
    char string1[100] = "This is a really long string!";
    char string2[100];
    strCopy(string1,string2);
    capitalize(string2);
    printf("The original string is \"%s\"\n", string1);
    printf("The capitalized string is \"%s\"\n", string2);
}

However, when I tried running the code it returned: 但是,当我尝试运行它返回的代码时:

The original string is "This is a really long string!" 原始字符串是“这是一个非常长的字符串!”
The capitalized string is "This is a really long string!" 大写字符串是“这是一个非常长的字符串!”

strcopy does not seem to be the issue, as it copies the string1 correctly to string2 . strcopy似乎不是问题,因为它正确地将string1复制到string2

I don't understand why capitalize is not working, as it seems like it should be going through letter by letter and changing it to uppercase , if the letter falls within the ascii code for a lowercase letter. 我不明白为什么capitalize不起作用,因为看起来它应该逐字逐句并将其更改为大写 ,如果该字母属于小写字母的ascii代码。

I would greatly appreciate someone helping to point out where the error is. 我非常感谢有人帮助指出错误的位置。

Thanks in advance! 提前致谢!

The problem is that your capitilize function doesn't have a loop. 问题是你的capitilize函数没有循环。 You only ever capitalize the first letter of your string. 您只需将字符串的第一个字母大写。

You are on the right track but what you are looking for is this: 你走在正确的轨道上,但你正在寻找的是:

for (int i = 0; str[i] != '\0'; ++i) { // Here we check for the end condition of a string.
                                       // ie. Has the Null termination been reached?
   if (str[i] >= 'a' && str[i] <= 'z') {
      str[i] = str[i] - ('a' - 'A');
   }
}

Here is a live demo for you to play with! 这是一个现场演示供您玩!

Your capitalize() function only runs for one character. 您的capitalize()函数仅运行一个字符。 Try like this 试试这样吧

for (int i = 0 ; str[i] != '\0' ; ++i) {
    if (str[i] >= 97 && str[i] <= 122)
        str[i] = str[i] - 32;
}

perform the operation on every character until the '\\0' is found. 每个字符执行操作,直到找到'\\0'

Try this: 尝试这个:

void capitalize(char *str)
{
    int i = 0;
    while (str[i] != '\0')
    {
      if (str[i] >= 97 && str[i] <= 122) {
          str[i] = str[i] - 32;
      }
      i++;
    }
}

you need to fix capitalize(char *str) to check all characters of the string,not just single character.A use of pointer can save you from including <string.h> to use strlen() function,like the following: 你需要修复capitalize(char *str)以检查字符串的所有字符,而不仅仅是单个字符。使用指针<string.h>包含<string.h>以使用strlen()函数,如下所示:

#include <stdio.h>

void capitalize(char *str)
{
    char *p = str;
    while(*p++) {
        if (*p >= 97 && *p <= 122) {
            *p -= 32;
        }
    }
}

void strCopy(char *str2, char *str1)
{
    while (*str2) {
        *str1 = *str2;
        str2++;
        str1++;
    }
    *str1 = '\0';
}

int main(int argc, char **argv)
{
    char string1[100] = "This is a really long string!";
    char string2[100];
    strCopy(string1, string2);
    capitalize(string2);
    printf("The original string is \"%s\"\n", string1);
    printf("The capitalized string is \"%s\"\n", string2);
}

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

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