简体   繁体   English

在 C 的 Hello World 代码中添加什么?

[英]What to add to this Hello World code in C?

I am very new to programming with C and I can't find a way to add to this program in order to spell "Hello, World!"我对使用 C 编程非常陌生,我找不到添加到该程序以拼写"Hello, World!"的方法。 without deleting any lines of code here.不在这里删除任何代码行。

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

void modify_1(char *c);
void modify_2(char *c);

int main()
{
    char str1[10];
    char str2[15];
    printf("%s, %s!\n", str1, str2);
}

void modify_1(char *c)
{
    char *a_string = "hello";
}

void modify_2(char *c)
{
    char *a_string = "world";
}

Consider the following code (see here in onlineGDB):考虑以下代码(请参阅 onlineGDB 中的此处):

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

void modify_1(char *c);
void modify_2(char *c);

int main(void)
{
    char str1[10];
    char str2[15];

    modify_1(str1);
    modify_2(str2);
    printf("%s, %s!\n", str1, str2);
}

void modify_1(char *c)
{
    strcpy(c, "hello");
}

void modify_2(char *c)
{
    strcpy(c, "world");
}

There are a few changes:有一些变化:

  1. You actually call modify_1 and modify_2 (otherwise, why would they affect anything?)您实际上调用modify_1modify_2 (否则,它们为什么会影响任何事情?)
  2. Within the functions, you use strcpy to copy the content of the literal to the address.在函数中,您使用strcpy将文字的内容复制到地址。

I can't find a way to add to this program in order to spell "Hello, World!"我找不到添加到该程序中以拼写"Hello, World!"的方法。 without deleting any lines of code here.不在这里删除任何代码行。

Given that you cannot delete any lines, you need only add 2-lines of code to each of your modify_1 and modify_2 functions to (1) copy the string-literals to your array, and then (2) change the first characters from lowercase to uppercase, and then just call modify_1 and modify_2 before you print.鉴于您无法删除任何行,您只需向每个modify_1modify_2函数添加2 行代码即可 (1) 将字符串文字复制到数组中,然后 (2) 将第一个字符从小写更改为大写,然后在打印之前调用modify_1modify_2 For example:例如:

void modify_1 (char *c)
{
    char *a_string = "hello";
    strcpy (c, a_string);         /* copy a_string to your array */
    c[0] = c[0] + 'A' - 'a';      /* change 1st character to uppercase */
}

void modify_2 (char *c)
{
    char *a_string = "world";
    strcpy (c, a_string);         /* same thing */
    c[0] = c[0] + 'A' - 'a';
}

After making the changes, you simply call the functions before your print statement in main() , eg进行更改后,您只需在main()中的 print 语句之前调用函数,例如

int main()
{
    char str1[10];
    char str2[15];

    modify_1 (str1);
    modify_2 (str2);

    printf("%s, %s!\n", str1, str2);
}

Example Use/Output示例使用/输出

$ ./bin/modify_12
Hello, World!

See ASCII Table and Description to understand how the conversion from lowercase to uppercase takes place.请参阅ASCII 表和说明以了解从小写字母到大写字母的转换是如何发生的。

This is probably not what is intended by the person that gave you the assignment but the simplest solution is to add two lines in main and leave the rest unchanged (and unused:-)这可能不是给您分配任务的人的意图,但最简单的解决方案是在main中添加两行并保持 rest 不变(并且未使用:-)

int main()
{
    char str1[10];
    char str2[15];
    strcpy(str1, "Hello");  // Add this line
    strcpy(str2, "World");  // Add this line
    printf("%s, %s!\n", str1, str2);
}

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

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