在c中仍然使用argc、argv[]时,如何提示用户输入?

在c中仍然使用argc、argv[]时,如何提示用户输入?,c,arrays,prompt,cs50,getstring,C,Arrays,Prompt,Cs50,Getstring,所以我对编程完全陌生,我甚至不确定我问的是否正确。只要输出与输入匹配,代码就完全符合我的要求。它应该只吐出你输入的单词的第一个字母 问题是我想提示用户输入并获取一个字符串,然后将其输入到我猜的数组中?现在,我的程序让用户在运行程序时将输入放在同一行。我不知道该如何实现这一点,也不知道该如何在谷歌中使用正确的措辞 include <stdio.h> include <cs50.h> include <ctype.h> int main (int argc,

所以我对编程完全陌生,我甚至不确定我问的是否正确。只要输出与输入匹配,代码就完全符合我的要求。它应该只吐出你输入的单词的第一个字母

问题是我想提示用户输入并获取一个字符串,然后将其输入到我猜的数组中?现在,我的程序让用户在运行程序时将输入放在同一行。我不知道该如何实现这一点,也不知道该如何在谷歌中使用正确的措辞

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


int main (int argc, string argv[])
{
    for (int i = 1; i < argc; i++)
    {

        printf("%c", toupper(argv[i][0]));
    }
    printf("\n");
}

我想我主要需要知道语法,如果我解释得太透彻了,对不起。提前感谢您的帮助,并让我知道您是否需要我澄清anthing/

以从用户处获取字符串输入,以下是我知道的方法:

scanf%s,str; 例:


请注意,您需要为此包含必要的标题。

如果您只需要吐出用户输入的每个字符串的第一个字母的大写版本,则不需要数组来存储任何输入;您只需从输入流中读取下一个字符,然后决定是否显示它

使用printf将提示写入标准输出流,即控制台:

printf( "Gimme some words: " );
然后使用getchar读取下一个输入字符,直到我们看到换行符或EOF已经发出信号,这意味着您需要某种循环。对于本例,For循环有点自然:

for ( int c = getchar(); c != '\n' && c != EOF; c = getchar() )
{
  // process input
}
for循环的结构是:

现在,您需要决定是否打印当前字符。如果要打印用户输入的每个字符串的第一个字符,则需要确定字符串中的第一个字符的含义。对于本例,我们将简单地假设字符串是由空格、制表符等分隔的非空白字符序列。。因此,基本上,您希望打印一个或多个空白字符之后的第一个非空白字符。这意味着您需要跟踪最后一个字符是否为空白。最简单的方法是声明第二个变量来保存以前读取的字符:

for ( int c = getchar(), last = ' '; c != '\n' && c != EOF; last = c, c = getchar() )
{
  // process input
}
#include <ctype.h> // for isspace function declaration
...
int c = getchar();
if ( isspace( c ) )
{
  // c is a whitespace character
}
在本例中,我们扩展expr1以声明和初始化第二个变量last。我们第一次将last初始化为一个空格,这样我们就有了一些有效的测试对象

我们还扩展了expr3,在读取下一个字符之前用当前值c更新last

现在,我们只需要决定是否打印当前字符。我们的规则是,如果前一个字符是空白,而当前字符不是空白,那么打印当前字符

有一个标准库函数isspace,如果参数是空白字符,它将返回true:

for ( int c = getchar(), last = ' '; c != '\n' && c != EOF; last = c, c = getchar() )
{
  // process input
}
#include <ctype.h> // for isspace function declaration
...
int c = getchar();
if ( isspace( c ) )
{
  // c is a whitespace character
}
下面是一个完整的示例:

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

int main( void )
{
  printf( "Gimme a bunch of words: " );
  for ( int c = getchar(), last = ' '; c != '\n' && c != EOF; last = c, c = getchar() )
  {
    if ( isspace( last ) && !isspace( c ) )
      putchar( toupper( c ) );
  }
  putchar( '\n' );

  return 0;
}
这个版本根本不使用argc和argv,事实上在main的定义中省略了它们。你可以把这两种方法结合起来;使用命令行参数(如果有);如果不是,则提示用户输入:

int main( int argc, char **argv )
{
  if ( argc > 1 )
  {
    for ( int i = 1; i < argc; i++ )
      putchar( toupper( argv[i][0] ) );
    putchar( '\n' );
  }
  else
  {
    printf( "Gimme some words: " );
    for ( int c = getchar(), last = ' '; c != '\n' && c != EOF; last = c, c = getchar() )
    {
      if ( isspace( last ) && !isspace( c ) )
        putchar( toupper( c ) );
    }
    putchar( '\n' );
  }
  return 0;
}


@rsp字符串是char*的typedef,它是头的一部分。大多数C程序员都非常讨厌它。@rsp在CS50中,他们使用字符串作为char*的typedef,这是完全疯狂的,但它确实有效。至于问题本身,如果您听说过这些函数,您是否想知道如何使用scanf或fgets读取用户输入?我建议你按照这里的解释去做。你正在使用你的程序的参数作为输入。你必须声明另一个字符串数组,并在你的主函数体中用scanf填充它。@一些程序员,他只需要读一些关于C的东西。他没有。在此之前,任何帮助都是无意义的。不推荐使用sgets。请修复格式。除了他们不应该使用gets,因为该函数容易发生缓冲区溢出,并且不再使用scanf的事件缓冲区溢出:scanf%49s,input;
for ( int c = getchar(), last = ' '; c != EOF; last = c, c = getchar() )
{
  if ( isspace( last ) && !isspace( c ) )
    printf( "%c", toupper( c ) );
}
#include <stdio.h>
#include <ctype.h>

int main( void )
{
  printf( "Gimme a bunch of words: " );
  for ( int c = getchar(), last = ' '; c != '\n' && c != EOF; last = c, c = getchar() )
  {
    if ( isspace( last ) && !isspace( c ) )
      putchar( toupper( c ) );
  }
  putchar( '\n' );

  return 0;
}
$ ./first
Gimme a bunch of words: this is a test of the emergency broadcast system
TIATOTEBS
int main( int argc, char **argv )
{
  if ( argc > 1 )
  {
    for ( int i = 1; i < argc; i++ )
      putchar( toupper( argv[i][0] ) );
    putchar( '\n' );
  }
  else
  {
    printf( "Gimme some words: " );
    for ( int c = getchar(), last = ' '; c != '\n' && c != EOF; last = c, c = getchar() )
    {
      if ( isspace( last ) && !isspace( c ) )
        putchar( toupper( c ) );
    }
    putchar( '\n' );
  }
  return 0;
}
$ ./first this is a test of the emergency broadcast system
TIATOTEBS
$ ./first
Gimme a bunch of words: This is a test of the emergency broadcast system
TIATOTEBS