0

I want to do a program that ask to the user to give one character, then enter... until he wants to stop by pressing enter and no caracters.

Then, the program will say: "you gave the caracters ...."

for example:

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

result: You gave the caracters: kl

My code doesnet work because when i just press enter, nothing happen. Here is the code:

#include <stdio.h>
#define N 1000

int main() {

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

    while (str[i] != '\n') {
        printf("element number str[%d] : ", i);
        scanf("%s", &str[i]);
        i++;
    }

    printf("The string is: ");

    while (j < i) {

        printf("%s", str[j]);
        j += 1;
    }

    return 0;
}
CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • First Read the warning when you compile the code. replace `%s` with `%c`. before scanning what is `str[i]` in `while (str[i] != '\n')` ? garbage. Initialize it first or something else – Achal Dec 17 '17 at 16:41
  • use fgetc instead to read one char at a time. – AndersK Dec 17 '17 at 16:43
  • array is uninitialised, has random values – Jacek Cz Dec 17 '17 at 17:02
  • while (str[i] != '\n') is just a way to stop the program when the user press only enter. Isnt it the good way? –  Dec 17 '17 at 17:08

2 Answers2

0

As stated in this answer scanf will not return until you give it a string, i.e. it skips whitespace.

As suggested in the answer and in general, using fgets is the better option.

Edit: A way to accomplish what you want would look like this:

#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;
}

In the fgets you use the number 3 because pressing enter gives both a newline character [/n] and a return carriage [/r].

T.Palludan
  • 176
  • 1
  • 2
  • 13
  • I can't use fgets. I just can use basic tools. Maybe it's possible to do something which works just by changing one or two lines in my code? –  Dec 17 '17 at 17:04
  • The only thing that wasn't using stdio.h was the for loop with strlen. Check my updated answer - is that okay? Otherwise you will have to define what basic tools are. – T.Palludan Dec 17 '17 at 17:50
0

You can do it with c = getchar(); or c = fgetc(stdin) function:

#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;
}

OUTPUT:

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!                                                                                                                                                                                                                 

Or you can use your original 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;
}

OUTPUT:

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         
sg7
  • 6,108
  • 2
  • 32
  • 40
  • No... But I really think i can do it without any functions!! The only problem is the (!= '\n' ) that doesnt work –  Dec 17 '17 at 17:14
  • @nolw38 Ok! You can do it with `scanf`. Working example shown. – sg7 Dec 17 '17 at 17:29