Search code examples
cgetcharputchar

Using putchar() and getchar() to print individual characters


I'm trying to use putchar() and getchar() to read in a string of characters from the user, and then my loop will print each character three times, print a new line, etc etc, until every character in the variable "userInput" has been printed. When I try to compile my program I receive the following errors:

warning: assignment makes pointer from integer without a cast [enabled by default]
 userInput = getchar();
           ^
warning: comparison between pointer and integer [enabled by default]
 while(counter < strlen(userInput))
               ^
error: array subscript is not an integer
     putchar(userInput[counter]);
                      ^
error: array subscript is not an integer
     putchar(userInput[counter]);
                      ^
error: array subscript is not an integer
     putchar(userInput[counter]);
                      ^

How can I fix these errors? I'm new to C and can't figure out what it means by the pointer cast error, and why my counter variable isn't working.

My code is as follows:

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

int main(void)
{
    char *userInput;
    int *counter = 0;

    printf("Enter a string of characters: ");
    userInput = getchar();


    while(counter < strlen(userInput))
    {
        putchar(userInput[counter]);
        putchar(userInput[counter]);
        putchar(userInput[counter]);
        printf("\n");
        counter++;
    }    
}

Solution

  • I'm not really sure why you want to do this, but if I had to do it, I'll probably do it like this -

    01 #include <stdio.h>
    02 int main()
    03 {
    04     char userInput[100],c;
    05     int length, i=0;
    06 
    07     printf("Enter a string of characters: ");
    08     while(c=getchar()){
    09         if(c=='\n')
    10             break;
    11          userInput[i++] = c;
    12      }
    13      userInput[i]='\0';
    14      length = i;
    15  
    16      for(i=0; i<length; i++){
    17          putchar(userInput[i]);
    18          putchar(userInput[i]);
    19          putchar(userInput[i]);
    20          putchar('\n');
    21      }
    22      return 0;
    23  }
    

    I've added line numbers for better understanding. Here's what I did -

    • I don't need any other header file except stdio.h, so removed extras.
    • You probably wanted a string (array of characters), so you have to define it like in line 04, as a character array. As getchar() returns a character, assigning it to a char pointer would give errors. Additionally I declared a variable "c" which you'll see later.
    • Instead of counter, I'm using the standard i, and declared a variable length to store the string length.
    • Interesting thing in line 08, while(c=getchar()). What this does is, it keeps reading a character and storing it in the variable c as long as a character exists in the input stream (entered by the user).
    • In line 09, I'm checking if c contains a newline (i.e. the user pressed Enter/Return key). If that's the case, I stop reading the string and get out of the loop using the break keyword in line 10.
    • In line 11, I store the character c at the ith index of the userInput array, and increment i at the same time. You could do the same thing in two lines two.
    • In line 13, the value of i will be the input string's length. At the last index, I store a special character '\0' which is the null terminator. You can look it up to know more about it.
    • In line 14, I store the value of i in length variable.
    • From line 16-21, I run a simple for loop from i=0 to length, and in each iteration I print the character 3 times as you wanted to. In line 20, instead of printf(), I use the putchar() to print a new line, as '\n' is a char, just to show you.

    So this does the task as you wanted to. I tried to make it as beginner friendly as I could, comment for any queries. Glad to help. :)