将字符保留在数组中

时间:2017-12-17 16:35:55

标签: c arrays char

我想做一个程序,要求用户给一个字符,然后输入......直到他想通过按Enter键停止而没有字符。

然后,程序会说:"你给了字符...."

例如:

shortestStreak = very high value, e.g. length of array + 1, or MAX_VALUE
streak = 0
for each value:
    if value = 1:
        streak++
    else:
        if streak > 0 and streak < shortestStreak:
            shortestStreak = streak
        streak = 0
if streak > 0 and streak < shortestStreak:
    shortestStreak = streak
if shortestStreak > length of array:
    print 'No ones found'
else:
    print shortestStreak

结果:你给了字符:kl

我的代码确实有效,因为当我按下回车时,什么也没发生。这是代码:

give the caracter 1: k + enter
give the caracter 2: l + enter
give the caracter 3: just enter ('\n')

2 个答案:

答案 0 :(得分:0)

this answer中所述,scanf将不会返回,直到你给它一个字符串,即它跳过空格。

正如答案中所建议的那样,使用fgets是更好的选择。

编辑:实现目标的方法如下:

#include <stdio.h>
#define N 1000

int main() {

int i = 0;
int j = 0;
char str[N];

do {
    printf("element number str[%d] : ", i);
    fgets(&str[i], 3, stdin);
    i++;
} while (str[i - 1] != '\n');

printf("The string is: ");

while (i > j) {
    printf("%c", str[j]);
    j++;
}

return 0;
}

在fgets中你使用数字3因为按Enter键同时给出一个换行符[/ n]和一个回车[/ r]。

答案 1 :(得分:0)

您可以使用c = getchar();c = fgetc(stdin)功能:

执行此操作
#include <stdio.h>
#define N 1000

int
main ()
{
  int i = 0;
  int j = 0;
  int c;

  char str[N];

  while (1)
  {
    c = fgetc(stdin); // or c = getchar();

    if ( (c != EOF) && (c != 0x0A ) ) // 0x0A = 'nl' character
    {
      str[i] = (char) c;
      printf ("element number str[%d]=%c \n", i, str[i++] );
    }
    else 
    {
     str[i] = 0;  
     break;
    }

  }

  printf ("The string is: %s", str);

  return 0;
}

输出:

This is my string!                                                                                                                                                                                                                                
element number str[1]=T                                                                                                                                                                                                                           
element number str[2]=h                                                                                                                                                                                                                           
element number str[3]=i                                                                                                                                                                                                                           
element number str[4]=s                                                                                                                                                                                                                           
element number str[5]=                                                                                                                                                                                                                            
element number str[6]=i                                                                                                                                                                                                                           
element number str[7]=s                                                                                                                                                                                                                           
element number str[8]=                                                                                                                                                                                                                            
element number str[9]=m                                                                                                                                                                                                                           
element number str[10]=y                                                                                                                                                                                                                          
element number str[11]=                                                                                                                                                                                                                           
element number str[12]=s                                                                                                                                                                                                                          
element number str[13]=t                                                                                                                                                                                                                          
element number str[14]=r                                                                                                                                                                                                                          
element number str[15]=i                                                                                                                                                                                                                          
element number str[16]=n                                                                                                                                                                                                                          
element number str[17]=g                                                                                                                                                                                                                          
element number str[18]=!                                                                                                                                                                                                                          
The string is: This is my string!                                                                                                                                                                                                                 

或者您可以使用原始scanf("%s", &str1);

#include <stdio.h>
#define N 1000

int main ()
{
    int i = 0;
    int k = 0;
    int c;
    int len;

    char str[N];
    char str1[N];

    scanf("%s", &str1);

    len = strlen(str1); 

    for(k = 0; k < len; k++)
    {
       c = str1[k];     

       if ( (c != EOF) && c != '\n') // EOF will work for ^D on UNIX
       {
          str[i] = (char) c;
          printf ("element number str[%d]=%c \n", i, str[i++] );
       }
       else 
       {
         str[i] = 0;  
         break;
       }
    }   

    printf ("The string is: %s", str);

    return 0;
}

输出:

12345                                                                                                                                                                                                                                             
element number str[1]=1                                                                                                                                                                                                                           
element number str[2]=2                                                                                                                                                                                                                           
element number str[3]=3                                                                                                                                                                                                                           
element number str[4]=4                                                                                                                                                                                                                           
element number str[5]=5                                                                                                                                                                                                                           
The string is: 12345