Inputting String in C programming language

In C programming language, the strings doesn't have a special data type on it's own. So it's usually represented with characters itself.
Usually an array of characters is used to represent characters like this:

``
int char[1000];
``

Characters can be read by variety of methods like:

1. scanf("%s", char);

It can read a single string, but reading strings with spaces is not supported by scanf operators

2. Then there is getline() function which is defined in C programming lanaguage book. It's actually part of
the build in library.

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

int main()
{
  int bytes_read;
  int nbytes = 100;
  char *my_string;

  puts ("Please enter a line of text.");

  /* These 2 lines are the heart of the program. */
  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getline (&my_string, &nbytes, stdin);

  if (bytes_read == -1)
    {
      puts ("ERROR!");
    }
  else
    {
      puts ("You typed:");
      puts (my_string);
      printf("%d", strlen(my_string));
    }

  return 0;
}

```

3. There is gets() functions which is generally considered as unsafe
4. fgets() is more safe as it provides a bounded input

```
fgets(string, 1000, stdin);
```

For more details check [this stackoverflow answer](https://stackoverflow.com/questions/2008173/writing-secure-c-and-secure-c-idioms)

As a bonus, I am sharing how to `find Pangram of a given string in C`

{% highlight linenos %}
#include<stdio.h>
#include<string.h>
#define MAX_LIMIT 1000

int main() {
    char string[MAX_LIMIT];
    int character_hash[26]={0};
    int pancount = 0;
    
    fgets(string, 1000, stdin);


    for(int i=0; i<strlen(string); i++) {
        if('a'<=string[i] && string[i]<='z')  {
            character_hash[string[i]-'a'] += 1;
        }
        else if('A'<=string[i] && string[i]<='Z') {
            character_hash[string[i]-'A'] += 1;
        }
    }
    
    for(int i=0;i<26;i++){
        if(character_hash[i]==0) {
            pancount=1;   
        }
    }

    if(pancount==0) {
        printf("Pangram");
    }
    else if(pancount==1) {
        printf("Not Pangram\n");
        for(int i=0;i<26;i++) {
            if(character_hash[i]==0) {
            printf("%c ", i+'a'); }
        }
    }
    
}

{% endhighlight %}

Last updated