简体   繁体   English

仅使用 scanf 从用户接收字符串并打印。 C语言

[英]Receiving a string from user with only scanf and printing it. C language

Forgive weird formatting.原谅奇怪的格式。 It's my first post.这是我的第一篇文章。 In the assignment, I have to receive a string from the user using scanf("%c"), store it into an array (for example, string[5]), and print the entered string using scanf("%s). Everything I've tried has resulted in the string not being printed properly or only the first word. Any help is appreciated.在作业中,我必须使用 scanf("%c") 从用户那里接收一个字符串,将其存储到一个数组中(例如,string[5]),然后使用 scanf("%s) 打印输入的字符串。我尝试过的一切都导致字符串无法正确打印或仅打印第一个单词。感谢任何帮助。

#include <stdio.h>
int main(void)
{
  char string[81];
  int i = 0;

  printf("Please enter a string. ");
  scanf("%c", string);

 while (string[i+1] != '\0')
 {
   printf("%s", i);
   i++;
 }
 return 0;
}

You can use a char to hold each entered character, but it's a little tricky as you actually use a pointer to char.您可以使用 char 来保存每个输入的字符,但这有点棘手,因为您实际上使用了指向 char 的指针。 You also need to change your final printf statement:您还需要更改最终的 printf 语句:

#include <stdio.h>
int main(void) {
    char string[81];
    int i = 0;
    char placeholder = '0';
    char* a = &placeholder;           //pointer to char for scanf to load
    printf("Please enter a string: ");
    while (*a != '\n' && i + 1 < 81) {    //deref pointer for comparison (&& size limit)
        scanf("%c", a);
        string[i] = *a;               //copy char into cstring array (deref again)
        i++;
    }
    string[i] = '\0';                 //add the null terminator
    i = 0;                            
    while (string[i+1] != '\0') {
        printf("%c", string[i]);     //this prints char by char
        i++;
    }
    printf("\n");
    return 0;
}

Edit[See comments]编辑[查看评论]

Well print the entered string using scanf("%s) couldn't get that but I think you're trying to do this without inputting all string at once. This might be the thing that you want:好吧,使用 scanf("%s) 打印输入的字符串无法做到这一点,但我认为您正在尝试执行此操作而无需一次输入所有字符串。这可能是您想要的:

#include <stdio.h>
int main(void)
{
  int somelimit = 81;
  char string[somelimit];
  int i = 0;
  int ch;
  printf("Please enter a string.\n");
  while((ch = getchar()) != '\n' && ch != EOF ) {
    if (i < somelimit) {
       string[i++] = ch;
    }
  }
  string[i] = '\0';
  i = 0;

  while(string[i] != 0)
  {
    printf("%c",string[i]);
    ++i;
  }
  printf("\n");

 return 0;
}

Note that you need a buffer limit for this.请注意,您需要为此设置缓冲区限制。

scanf("%c", string); only reads 1 character.只读取 1 个字符。 @Jonathan Leffler @乔纳森莱弗勒

Keep reading with getchar() until buffer full, end-of-line, end-of-file, input error.使用getchar()继续阅读,直到缓冲区满、行尾、文件尾、输入错误。

#include <stdio.h>

int main(void) {
  char string[81];

  printf("Please enter a string. ");

  int ch;
  size_t i = 0;
  while((i < sizeof string - 1) && (ch = getchar()) != '\n' && ch != EOF) {
    string[i++] = (char) ch;
  } 
  string[i] = '\0';  // Append null character to form a _string_

  printf("<%s>\n", i);
  return 0;
}

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

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