C Program to demonstrate strspn function.

Write a C Program to demonstrate strspn function.
The strspn() function returns the index of the first character in string1 that doesn’t match any character in string2. If all the characters of the string2 matched with the string1, strspn returns the length of the string1. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
int spn;
char str1[] = "1234hjbcde09i";
char str2[] = "12345";
char str3[] = "a12d3456fd";
char char_list[] = "1234567890";
clrscr();
//It returns 4
spn = strspn (str1,char_list);
printf ("nnThe length of the initial numbers for str1 is %d.n",spn);

//It returns 5
spn = strspn (str2,char_list);
printf ("nThe length of the initial numbers for str2 is %d.n",spn);

//It returns 0
spn = strspn (string3,char_list);
printf ("nThe length of the initial numbers for string3 is %d.n",spn);
return 0;
}

Read more c programs
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to demonstrate ceil function.

Write a C program to demonstrate ceil function.ceil is in the C math.h library.
ceil function rounds up the smallest next integral value.
Example:ceil(2.0) is 2.0
ceil(2.1) is 3.0
ceil(2.2) is 3.0


ceil(2.9) is 3.0
Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/




#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
double number, result;
clrscr();
printf("Enter a number to round it upn");
scanf("%lf",&number);

result = ceil(number);

printf("nOriginal number = %lfn", number);
printf("nNumber rounded up = %lfn", result);
getch();
return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to draw a circle using c graphics.

Write C program to draw a circle using c graphics.
In this program we use the graphics.h library function to draw a circle. To use graphics.h, we have to install the drivers in to the the system by using the initgraph() function. In the program circle is the graphic function , which takes three parameters, first two are co-ordinates and the third one is the radius of the circle. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <graphics.h>

main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI");

circle(25, 25, 100);

getch();
closegraph();
return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Chapter 2 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 2 of the book for easy reference.

Chapter 2: Types, Operators and Expressions

  1. Exercise 2-1.Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
    Solution to Exercise 2-1.
  2. Exercise 2-2.Write a loop equivalent to the for loop above without using && or || .
    Solution to Exercise 2-2.
  3. Exercise 2-3.Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F .
    Solution to Exercise 2-3.
  4. Exercise 2-4.Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2 .
    Solution to Exercise 2-4.
  5. Exercise 2-5.Write the function any(s1,s2) , which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2 . (The standard library function strpbrk does the same job but returns a pointer to the location.)
    Solution to Exercise 2-5.
  6. Exercise 2-6.Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
    Solution to Exercise 2-6.
  7. Exercise 2-7.Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
    Solution to Exercise 2-7.
  8. Exercise 2-8.Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.
    Solution to Exercise 2-8.
  9. Exercise 2-9.In a two’s complement number system, x &= (x-1) deletes the rightmost 1-bit in x . Explain why. Use this observation to write a faster version of bitcount .
    Solution to Exercise 2-9.
  10. Exercise 2-10.Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .
    Solution to Exercise 2-10.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to demonstrate how the escape characters work.

C Program to demonstrate how the escape characters work.Escape character is the single character, and it is compiled as the character constant.
Escape characters are used to solve the c format problems.
An escape sequence is denoted by using the backslash () as an escape character, followed by a single character indicating the nonprintable character desired.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>
main()
{
clrscr();
printf("Escape operators");

printf("Example program to show the escape characters:nn");
printf("t here is tab");
printf("n Here starts new line");
getch();
}

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Chapter 1 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 1 of the book for easy reference.

Chapter 1: A Tutorial Introduction

  1. Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program to see what error messages you get.
    Solution to Exercise 1-1.
  2. Exercise 1-2. Experiment to find what happens when printf’s argument string contains c, where c is some character not listed above.
    Solution to Exercise 1-2.
  3. Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
    Solution to Exercise 1-3.
  4. Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
    Solution to Exercise 1-4.
  5. Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
    Solution to Exercise 1-5.
  6. Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1.
    Solution to Exercise 1-6.
  7. Exercise 1-7. Write a program to print the value of EOF.
    Solution to  Exercise 1-7.
  8. Exercise 1-8.Write a program to count blanks, tabs, and newlines.
    Solution to  Exercise 1-8.
  9. Exercise 1-9.Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
    Solution to  Exercise 1-9.
  10. Exercise 1-10. Write a 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 unambiguous way.
    Solution to  Exercise 1-10.
  11. Exercise 1-11.How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
    Solution to  Exercise 1-11.
  12. Exercise 1-12.Write a program that prints its input one word per line.
    Solution to  Exercise 1-12.
  13. Exercise 1-13.Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
    Solution to  Exercise 1-13.
  14. Exercise 1-14.Write a program to print a histogram of the frequencies of different characters in its input.
    Solution to  Exercise 1-14.
  15. Exercise 1-15.Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
    Solution to  Exercise 1-15.
  16. Exercise 1-16.Revise the the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
    Solution to  Exercise 1-16.
  17. Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
    Solution to  Exercise 1-17.
  18. Exercise 1-18. Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
    Solution to  Exercise 1-18.
  19. Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write program that reverse thes its input a line at a time.
    Solution to  Exercise 1-19.
  20. Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
    Solution to  Exercise 1-20.
  21. Exercise 1-21. Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
    Solution to  Exercise 1-21.
  22. Exercise 1-22. Write a program to “fold” long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
    Solution to  Exercise 1-22.
  23. Exercise 1-23. Write a program to remove all comments from a C program. Don’t forget to handle quoted strings and character constants properly. C comments do not nest.
    Solution to  Exercise 1-23.
  24. Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don’t forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)
    Solution to  Exercise 1-24.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

(c) www.c-program-example.com

K & R C Programs Exercise 3-3.

K and R C, Solution to Exercise 3-3:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to expand short hand notations like a-z in the string s1 into the equivalent complete list abc—xyz in s2. Allowed for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arranged that a leading or trailing – is taken literally. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include
#include

void expand(char * s1, char * s2);

int main(void) {
char *s[] = { "a-z-", "z-a-", "-1-6-",
"a-ee-a", "a-R-L", "1-9-1",
"5-5", NULL };
char result[100];
int i = 0;

while ( s[i] ) {

expand(result, s[i]);
printf("Unexpanded: %sn", s[i]);
printf("Expanded : %sn", result);
++i;
}

return 0;
}



//expand: expand short hand notations in s1 into string s2
void expand(char * s1, char * s2) {
static char upper_alph[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char lower_alph[27] = "abcdefghijklmnopqrstuvwxyz";
static char digits[11] = "0123456789";

char * start, * end, * p;
int i = 0;
int j = 0;


/* Loop through characters in s2 */

while ( s2[i] ) {
switch( s2[i] ) {
case '-':
if ( i == 0 || s2[i+1] == '' ) {

s1[j++] = '-';
++i;
break;
}
else {


if ( (start = strchr(upper_alph, s2[i-1])) &&
(end = strchr(upper_alph, s2[i+1])) )
;
else if ( (start = strchr(lower_alph, s2[i-1])) &&
(end = strchr(lower_alph, s2[i+1])) )
;
else if ( (start = strchr(digits, s2[i-1])) &&
(end = strchr(digits, s2[i+1])) )
;
else {

fprintf(stderr, "EX3_3: Mismatched operands '%c-%c'n",
s2[i-1], s2[i+1]);
s1[j++] = s2[i-1];
s1[j++] = s2[i++];
break;
}


p = start;
while ( p != end ) {
s1[j++] = *p;
if ( end > start )
++p;
else
--p;
}
s1[j++] = *p;
i += 2;
}
break;

default:
if ( s2[i+1] == '-' && s2[i+2] != '' ) {

++i;
}
else {

s1[j++] = s2[i++];
}
break;
}
}
s1[j] = s2[i];
}


Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K&R C Program Exercise 1-01(a)

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. Experiment with leaving out parts of the program to see what error messages you get.Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include<stdio.h>

main()
{
printf("hello, world");
}


/*Output: The new line character (n) is missing.
This leaves the cursor at the end of the line.*/

Read more Similar C Programs
C Basic

K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com