CS50:pset2 /缩写:-我有能正常工作的代码,但我觉得我在设置数组大小时捷径 [英] CS50: pset2 / initials:- I've got code that works but I feel like I am taking a shortcut with setting my array size

查看:93
本文介绍了CS50:pset2 /缩写:-我有能正常工作的代码,但我觉得我在设置数组大小时捷径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在研究CS50中缩写问题的不太舒适版本,并且从非常冗长的代码开始之后,我设法将其缩减为:

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

int c = 0;

int main(void)
{
字符串名称= get_string();
int n = strlen(name);

字符首字母[10];

//第一个字母始终是第一个首字母缩写
首字母[0] =名称[0];

//计算字母以寻找空格,然后将
//后面的第一个字母添加到首字母数组
中,用于(int j = 0; j< n; j ++)
{
if(name [j] == 32)
{
c + = 1;
首字母[c] + =名称[j + 1];
}
}

//打印出首字母
for(int k = 0; k< = c; k ++)
{
printf(%c,toupper(initials [k]));
}

printf( \n);
}

它看起来像过去了,但我觉得自己正在应对我只是从空中挑出[10]作为初始阵列大小,我知道这不是一个好习惯。为了使它更好一点,我尝试运行一个 for循环以遍历名称字符串并累加空格。然后,我想使数组[spaces + 1]好像有2个空格,那么将有3个缩写。我正在尝试的代码是:

  string name = get_string(); 
int n = strlen(name);

for(int i = 0; i< n; i ++)
{
if(name [i] == 32)
{
空格+ = 1;
}

}

我以为是下一行是'char initials [spaces + 1]',但即使在我可以执行此操作之前,仅使用此'for'循环编译代码也会在我上传代码进行检查时返回失败(尽管它编译没有问题)。即使我不使用任何 for循环输出,仅凭事实就给了我这个错误。



我要去哪里了?



对此将提供任何帮助。



谢谢!


然后,我想使数组[spaces + 1]好像有2个空格,那么会有3个首字母缩写


C字符串以null终止,因此您还需要为null终止符分配空间,空格+1 + 1


仅使用此 for循环编译我的代码,当我上传代码进行检查时返回失败(尽管它编译没有问题)。即使我不使用任何 for循环输出,仅凭事实就给了我这个错误。


什么错误?它是编译还是不编译,您的文本是矛盾的。



请确保将空格初始化为零。






请注意,切勿在C代码中使用幻数。 if(name [i] == 32),对于那些无法按内存引用ASCII表的人来说,32是不合理的。此外,它不可移植到带有其他符号表的系统,这些符号表可能没有相同的索引号。改为:

  if(name [i] =='')


So I am working away on the 'less comfortable' version of the initials problem in CS50, and after beginning with very verbose code I've managed to whittle it down to this:

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

int c = 0;

int main(void)
{
    string name = get_string();
    int n = strlen(name);

    char initials[10];

    // first letter is always going to be the first initial
    initials[0] = name[0];

    // count through letters looking for spaces + add the first letter after a 
    // space to the initials array
    for (int j = 0; j < n; j++)
    {
        if (name[j] == 32)
        {
            c += 1;
            initials[c] += name[j+1];
        }
    }    

    // print out initials
    for (int k = 0; k <= c; k++)
    {
        printf("%c", toupper(initials[k]));
    }    

   printf("\n"); 
} 

As it stands like that it passes, but I feel like I am copping out a little cos I just pick [10] out of the air for the initial array size which I know isn't good practice. To make it a little 'better' I've tried to run a 'for' loop to iterate through the name string and add up the number of spaces. I then want to make the array [spaces + 1] as if there are 2 spaces then there will be 3 initials. The code I am trying for that is:

string name = get_string();
int n = strlen(name);

for (int i = 0; i < n; i++)
{
    if (name[i] == 32)
    {
        spaces +=1;
    } 

}      

The thought is that I then make 'char initials[spaces + 1]' on the next line, but even before I can do that, compiling my code with just this 'for' loop returns a fail when I upload it for checking (although it compiles no problem). Even if I don't use any of the 'for' loops output the mere fact it is there gives me this error.

Where am I going wrong?

Any help on this would be much appreciated.

Thanks!

解决方案

First of all, keep in mind that execution speed is most often more valuable than memory use. If you first go look for spaces and after that allocate memory, you have to iterate through the array twice. This is an optimization of memory use at the cost of execution speed. So it might make more sense to just allocate a "large enough" array of lets say 100 characters and keep the code that you have.

I then want to make the array [spaces + 1] as if there are 2 spaces then there will be 3 initials

Keep in mind that C strings are null terminated, so you need to allocate room for the null terminator too, spaces + 1 + 1.

compiling my code with just this 'for' loop returns a fail when I upload it for checking (although it compiles no problem). Even if I don't use any of the 'for' loops output the mere fact it is there gives me this error.

What error? Does it compile or does it not compile, your text is contradicting.

Make sure you initialize spaces to zero.


As a side note, never use "magic numbers" in C code. if (name[i] == 32), 32 is gibberish to anyone who can't cite the ASCII table by memory. In addition, it is non-portable to systems with other symbol tables that might not have the same index numbers. Instead write:

if (name[i] == ' ')

这篇关于CS50:pset2 /缩写:-我有能正常工作的代码,但我觉得我在设置数组大小时捷径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆