✍️
Today IL
  • Today I learned!
  • Deployment
    • Rolling, Canary, Blue-green deployments
    • Kubernetees Helm Charts
  • AI/ML
    • SeldonIO
    • Installing software in E2E cloud compute
    • Watching nvidia-smi
    • How does github copilot works?
    • composer library
    • Better to pass callback in fit_one_cycle
    • Eliza - demo
    • Helsinki-NLP Translation models
  • Fastai Learning
  • Python
    • Understanding get_image_files in fastai
    • Resizing an image to smaller size
    • Extracting a Json Object as List(regex)
    • f-strings debugging shortcut
    • Pytest
    • conda switch between python versions
    • Nested functions exception handling
  • Programming
    • Installing Linux Operating system
    • Certbots usage
    • Code highlighting in Google Docs
    • HTTP Methods
    • How to use vertical mouse?
    • HTTP Status Codes
    • Keycloak, Oauth, OpenID connect, SAML
    • Why should NPM packages be as small as possible?
    • Clean Architecture
    • REST vs gRPC
    • Keycloak concepts
    • what is proxy server and nginx?
    • Asymptotic Time Complexity
  • async/await
    • JavaScript Asynchronous operation
    • Lesson 2- Eventloops
    • Lesson 1- asyncio history
    • Lesson 3- using coroutines
    • Lesson 4- coroutines in hood
    • Python async/await
    • JavaScript
  • R Programming language
    • Facet_grid and Facet_wrap
    • geom_point
  • C programming language
    • Inputting String in C programming language
    • Checking if a element is passed as input or not?
  • Git/Github
    • give credits to other people
    • one time setting to easily use Github
    • Checkout to specific tag
    • git suggestions in PR
    • Using emojis in git commits
  • Databases
    • Postgres Database Dockercompose
    • TIL New SQL Operators - Except, UNION, Distinct
    • Analysing Performance of DB Queries
    • Querying Date ranges in postgres
    • Handling Database disconnects in SQLAlchemy
  • WITH NO EXCEPT
  • What is difference with JSON documents in postgres and Mongodb
Powered by GitBook
On this page

Was this helpful?

  1. C programming language

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 %}
Previousgeom_pointNextChecking if a element is passed as input or not?

Last updated 3 years ago

Was this helpful?