Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C

C

This section presents 51 C programs which can help the beginners learn the major language skills of C, a few good algorithms and a deeper understanding of how the standard libraries of the ANSI C has been implemented. Most of the programs are solutions to the problems given in the book, The C Programming Language, Brian W. Kernighan & Dennis M. Ritchie.

List of C Programs :

  1. Program to form a pyramid of numbers.
  2. Program for word count.
  3. Program to copy its input to its output, replacing each string containing one or more blanks by a single blank.
  4. Program to copy its input to its output, replacing each tab by \t, each backspace by \b and each backslash by \\. This makes tabs and backspaces visible in an unambiguos way.
  5. Program that prints its input one word per line.
  6. Program to print a histogram of the lengths of words in its input.
  7. Program to find the longest line.
  8. Program to print all input lines those are longer than 10 characters.
  9. Function tolower(a) to convert an upper case character to lower case and leave the rest of the characters unchanged.
  10. Function atoi(s) to convert a string to integer.
  11. Funtion htoi(s) to convert a string of hexadecimal digits into its equivalent integer value. The allowable digits are 0 through 9, a through f and A through F. The function returns -1 on encountering an invalid character.
  12. Function squeeze(s,c) to remove a specific character from a string.
  13. Function squeeze(s1,s2) that deletes each character in the first string that matches any character in the second string.
  14. Function any(s1,s2) that returns the first location in the first string where any character from the second string occurs, or -1 if the first string contains no characters from the second string.
  15. Function strcat(s,t) to concatenate t to the end of s, s must be big enough.
  16. Function that gets n bits from postion p.
  17. Function that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving other bits unchanged.
  18. Function that returns x with the n bits at position p inverted.
  19. Function bitcount(x) that counts the number of 1 bits in a number.
  20. Faster version of bitcount(x).
  21. Function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t.
  22. Program to print each line of its input that contains a particular pattern of strings.
  23. Function strindex(s,t) which returns the position of the rightmost occurence of t in s, or -1 if there is none.
  24. Function itoa(n,s) to convert a number to string.
  25. Function itob(n,s,b) to convert the integer n into a base b character representation in the string s.
  26. Function itoa(n,s,w) to convert a number to string and the converted number must be padded with blanks on the left if necessary to make it atleast w characters wide.
  27. Function atoi(s) to convert a string into number after skipping the white spaces in the beginning and terminating the conversion when first non-digit character is encountered.
  28. Function atof(s) that converts a string into floating point number after skipping the white spaces in the beginning and terminating the conversion when the first non-digit character is encountered.
  29. Function atof(s) extended to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
  30. Macro swap(t,x,y) that interchanges two arguments of type t.
  31. Function shellsort(array,n) based on Shell sort algorithm invented by D. L. Shell in 1959.
  32. Function quicksort(array,left,right) based on quicksort algorithm developed by C. A. R. Hoare in 1962.
  33. Function swap(&a,&b) to swap the values of two variables.
  34. Function strcpy(s,t) to copy the string t into s and return the copied string. (using pointers)
  35. Function strcmp(s,t) to return the difference of the characters at the first position where s and t disagree. (using pointers)
  36. Function strcat(s,t) to concatenate t to the end of s and return the concatenated string, s must be big enough. (using pointers)
  37. Function strncpy(s,t,n) to copy at most n characters of string t into s and return the copied string. It pads s with '\0' if t has fewer than n characters. s might not be null-terminated if the length of t is n or more. (using pointers)
  38. Function strncmp(s,t,n) to compare at most n characters and return the difference of the characters at the first position where s and t disagree. (using pointers)
  39. Function strncat(s,t,n) to concatenate at most n characters of t to the end of s and return the concatenated string, s must be big enough. (using pointers)
  40. Function strend(s,t) which returns 1 if the string t occurs at the end of the string s, and 0 otherwise. (using pointers)
  41. Program that echoes command line arguments.
  42. Function getint(&a) to break a stream of characters into integer values, one integer value per call.
  43. Function getint(&a) to break a stream of characters into integer values, one integer value per call such that '+' or '-' not followed by a digit is treated as invalid number.
  44. Function getfloat(&a) to break a stream of characters into float values, one float value per call such that '+' or '-' not followed by a digit is treated as invalid number.
  45. Program to count the occurrences of each C keyword.
  46. Function getword(word,lim) to return identifiers, keywords, preprocessor directives, character constants, string constants and ignore comments.
  47. Program that converts upper case to lower or lower case to upper, depending on the name it is invoked with.
  48. Program that prints arbitrary input in a sensible way. It prints non-graphic character in hexadecimal and break long text lines.
  49. Function minprintf(), a minimal version of printf().
  50. Function minscanf(), a minimal version of scanf().
  51. Program to compare two files, printing the first line where they differ.