C programming Language – Code snippets

C Programming Language, 2nd Edition

Compiling and running the sample codes using gcc :

gcc sample.c -o sample
./sample

Chapter 3 – Control Flow

1. Trim whitespace

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


int main()
{
  int n,i=0;
  char s[]="   HELLO THERE    ";
  printf("Before trim:%s\n",s);
  for(n=strlen(s)-1; n>=0; n--)
   if(s[n]!=' ' && s[n]!='\t' && s[n]!='\n') break;
  s[n+1]='\0';
  while(isspace(s[i++]));
  i=0;
  while(s[i]!='\0') s[i++];
  s[i]='\0';
  printf("After trim:%s\n",s);
  return n;
}

2. Binary search

#include<stdio.h>


int binsearch(int x, int v[], int n);

int main(int argc, char *argv[])
{
  int size=argc,found;
  int counter=0,x, v[size-2];
  x=atoi(argv[size-1]);
  if (argc<3) {printf("Usage: progname integer-list integer-to-be-searched\n"); return -1; }
  while(counter <= size-3)
  { v[counter]=atoi(argv[counter+1]);
    counter++; }
(found=binsearch(x,v,size-2))!=-1 ? printf("Found=%d\n",v[found]): printf("Not found\n");
 return 0;
}

int binsearch(int x, int v[], int n)

{
   int low, high, mid;
   low=0;
   high=n-1;
   while(low<=high)
     {
       mid=(low+high)/2;
       if(x < v[mid])  high=mid-1;
       else if (x > v[mid]) low=mid+1;
       else
         return mid;
     }
return -1;
}

3. Integer to string with argument parsing.


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define  SIZE  10

void itoa(int n, char s[]);
void reverse(char s[]);
int main(int argc, char *argv[])
 {
   char s[SIZE];
   int n;
   if(argc!=2) { printf("Usage: progrname integer\n"); return -1; }
   n=atoi(argv[1]);
   itoa(n,s);
   printf("String:%s\n",s);
   return 0;

 }

void itoa(int n, char s[])
 {
    int i, sign;
    if((sign=n)<0) n=-n;
    i=0;
    do {
         s[i++]=n%10 + '0';
       } while( (n/=10)>0);

 if (sign<0) s[i++]='-';
 s[i]='\0';
 reverse(s);
}

void reverse(char s[])
{
 int c, i, j;

  for(i=0,j=strlen(s)-1; i<j; i++,j--)
   {
        c=s[i];  s[i]=s[j];  s[j]=c;
   }
}

4. Reverse string and convert integer to string

#include<stdio.h>
#include<string.h>
#define SIZE 1000 

void reverse(char s[]);
void itoa(int n, char s[]);

int main()
{
  char s[SIZE]="HELLO WORLD!!";
  reverse(s);
  printf("%s\n",s);
  itoa(87690,s);
  printf("%s\n",s);
return 0;
}

void reverse(char s[])
{
 int c, i, j;

  for(i=0,j=strlen(s)-1; i<j; i++,j--)
   {
        c=s[i];  s[i]=s[j];  s[j]=c;
   }
}

void itoa(int n, char s[])
{
  int i, sign;
  if((sign=n)<0) n=-n;
  i=0;
      do {
           s[i++] = n%10 + '0';
         } while((n/=10)>0);
  if(sign<0) s[i++] = '-';
  s[i]='\0';
  reverse(s);
}

5. Command line argument parsing


#include<stdio.h>

int main(int argc, char *argv[])
{
  int i,counter;
  printf("Number of arguments:%d\n",argc);
  printf("Arguments character-by-character\n");
   for(counter=0;counter < argc ; counter++)
    { while(*argv[counter])
      printf("%c ",*argv[counter]++);
      printf("\n");
    }

printf("\n");
return 0;
}

6. Reverse a string

#include<stdio.h>
#include<string.h>
#define SIZE 1000 

void reverse(char s[]);

int main()
{
  char s[SIZE]="HELLO WORLD!";
  reverse(s);
  printf("%s\n",s);

return 0;
}

void reverse(char s[])
{
 int c, i, j;

  for(i=0,j=strlen(s)-1; i<j; i++,j--)
   {
        c=s[i];  s[i]=s[j];  s[j]=c;
   }
}

7. Sorting with shell sort method.


#include<stdio.h>
#define  SIZE  10
void shellsort(int v[], int n);

int main()
{
int counter;
int v[SIZE]={8,13,9,45,90,24,56,11,20,10};
shellsort(v,SIZE);
for(counter=0; counter<SIZE; counter++)
  printf("%d  ",v[counter]);
printf("\n");
return 0;
}

void shellsort(int v[], int n)
{
   int gap, i, j, temp;
   for(gap=n/2; gap>0; gap/=2)
     for(i=gap; i<n; i++)
       for(j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap)
        {
           temp=v[j];
           v[j]=v[j+gap];
           v[j+gap]=temp;
        }
}

8. using switch case


#include<stdio.h>

int main()
{
  int c, i, nwhite, nother,alpha, ndigit[10];
  nwhite=nother=alpha=0;
  for(i=0; i<10; i++) ndigit[i]=0;
  while((c=getchar())!=EOF)
  {
   switch(c)
    {
      case '0': case '1': case '2': case '3': case '4': case '5':
      case '6': case '7': case '8': case '9':
        ndigit[c-'0']++; break;
      case ' ': case '\n': case '\t': nwhite++; break;
      default:
          nother++; break;
     }
   }
printf("Digits =");
for(i=0; i<10; i++) printf(" %d", ndigit[i]);
printf(", white space =%d, other = %d\n", nwhite, nother);
return 0;
}

9. Double input – strip out non integers

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

#define  SIZE  10

int atoi(char s[]);

int main(int argc, char *argv[])
{
  int counter=0;
  char s[SIZE];
  if (argc!=2) {
    printf("Usage: progname integer\n");
    return 1;
  }
  while(*argv[1])  s[counter++]=(*argv[1]++);
  printf("Twice Result=%d\n",2*atoi(s));
return 0;
}
int atoi(char s[])
{
  int n, i,sign;

 for(i=0; isspace(s[i]); i++);

sign=(s[i]=='-')? -1: 1;
if(s[i] == '+' || s[i] == '-') i++;
 for(n=0; isdigit(s[i]); i++)
   n=10*n + (s[i]-'0');

return sign*n;
}

Reference –
https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628#reader_0131103628