K & R C Programs Exercise 5-3.

K and R C, Solution to Exercise 5-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 concatenate the two strings using the pointers
This program shows, how the standard library function “strcat” works!. 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>
/* str_cat: concatenate t to the end of s: pointer version */
void str_cat(char *s, char *t)
{
/* run through the destination string until we point at the terminating '' */
while('' != *s)
{

++s;
}

/* now copy until we run out of string to copy */
while('' != (*s = *t))
{
++s;
++t;
}

}
int main(void)
{
char Str1[8192] ;
char Str2[8192] ;
printf("n Enter the first string: n");
scanf("%s",Str1);
printf("n Enter the second string: n");
scanf("%s",Str2);
printf("String one is (%s)n", Str1);
printf("String two is (%s)n", Str2);

str_cat(Str1, Str2);
printf("The combined string is (%s)n", Str1);

return 0;
}


C BasicC StringsK and R C Programs ExerciseYou 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 Programs Exercise 5-2.

K and R C, Solution to Exercise 5-2:
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 get next floating point analog of getint.The routine getfloat is similar to the routine getint. getfloat skips whitespaces, records the sign, and stores the integer part of the number at the address in fp.
getfloat also handles the practional part of the number(but not scientific notation). the fractional part is added to *fp in the same fashion as the integer part. 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<ctype.h>
#include<math.h>

int getfloat(float *fp)
{
int ch;
int sign;
int fraction;
int digits;

while (isspace(ch = getch())) /* skip white space */
;

if (!isdigit(ch) && ch != EOF && ch != '+'
&& ch != '-' && ch != '.') {
ungetch(ch);
return 0;
}

sign = (ch == '-') ? -1 : 1;
if (ch == '+' || ch == '-') {
ch = getch();
if (!isdigit(ch) && ch != '.') {
if (ch == EOF) {
return EOF;
} else {
ungetch(ch);
return 0;
}
}
}

*fp = 0;
fraction = 0;
digits = 0;
for ( ; isdigit(ch) || ch == '.' ; ch = getch()) {
if (ch == '.') {
fraction = 1;
} else {
if (!fraction) {
*fp = 10 * *fp + (ch - '0');
} else {
*fp = *fp + ((ch - '0') / pow(10, fraction));
fraction++;
}
digits++;
}
}

*fp *= sign;

if (ch == EOF) {
return EOF;
} else {
ungetch(ch);
return (digits) ? ch : 0;
}
}

//for testing... try the different one!
#include<stdio.h>

int main(void)
{
int ret;

do {
float f;

fputs("Enter a number: ", stdout);
fflush(stdout);
ret = getfloat(&f);
if (ret > 0) {
printf("You entered: %fn", f);
}
} while (ret > 0);

if (ret == EOF) {
puts("Stopped by EOF.");
} else {
puts("Stopped by bad input.");
}

return 0;
}



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 Programs Exercise 5-1.

K and R C, Solution to Exercise 5-1:
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 get next integer from input into space, so that getint treats a + or – not followed by a digit as a valid representation of zero and fix it to push such a character back on the input. 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<ctype.h>

int getch(void);
void ungetch(int);

/* getint: get next integer from input into *pn */
int getint(int *pn)
{
int c, sign, sawsign;

while (isspace(c = getch())) /* skip white space */
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c); /* it's not a number */
return 0;
}
sign = (c == '-') ? -1 : 1;
if (sawsign = (c == '+' || c == '-'))
c = getch();
if (!isdigit(c)) {
ungetch(c);
if (sawsign)
ungetch((sign == -1) ? '-' : '+');
return 0;
}
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}
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 Programs Exercise 4-14.

K and R C, Solution to Exercise 4-14:
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.
Write C Program to swap two arguments using macros.
C Program to swap(t, x, y) that interchanges two arguments of type t using the block structure.

/***********************************************************
* 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>

#define swap(t, x, y)
do {
t safe ## x ## y;
safe ## x ## y = x;
x = y;
y = safe ## x ## y;
} while (0)

int main(void) {
int inum1, inum2;
double dnum1, dnum2;
char *ch1, *ch2;
printf("nEnter two Intgers:n");
scanf("%d%d",&inum1,&inum2);
printf("nIntegers before swap:n inum1= %dn inum2= %dn", inum1, inum2);
swap(int, inum1, inum2);
printf("nIntegers after swap:n inum1= %dn inum2= %dn", inum1, inum2);

printf("nEnter two Doubles:n");
scanf("%f%f",&dnum1,&dnum2);
printf("nDoubles before swap:n dnum1= %gn dnum2= %gn", dnum1, dnum2);
swap(double, dnum1, dnum2);
printf("nDoubles after swap:n dnum1= %gn dnum2= %gn", dnum1, dnum2);

printf("nEnter two Strings:n");
scanf("%s%s",ch1,ch2);
printf("n Strings before swap:n ch1= %sn ch2 = %sn", ch1, ch2);
swap(char *, ch1, ch2);
printf("nStrings after swap:n ch1= %sn ch2= %sn", ch1, ch2);

return 0;
}
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 Programs Exercise 4-13.

K and R C, Solution to Exercise 4-13:
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.
Write a C Program to reverse the string using the recursive methods.
In this program reverse determines the length of the string and then calls the reverser, which reverses the string s in place. 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>
/* reverse: reverse the string s in place */
void reverse(char s[])
{

void reverser(char s[], int i, int len);
reverser(s,0,strlen(s));
}

/* reverser: reverse string s in place recursive */
void reverser(char s[], int i, int len)
{
int c, j;
j = len - (i + 1);
if(i < j)
{
c = s[i];
s[i] = s[j];
s[j] = c;
reverser(s, ++i, len);
}
}

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 Programs Exercise 4-12.

K and R C, Solution to Exercise 4-12:
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.
Write a C program to convert an integer into a string by calling a recursive routine.Recursive routine is the programming technique that a routine invoking itself again and again. 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<math.h>
void itoa(int n, char s[]);
int main(void) {
char buffer[20];

//for testing!
printf("INT_MIN: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", buffer);

return 0;
}
/* itoa: convert n to characters in s; recursive */
void itoa(int n, char s[])
{
static int i;
if(n / 10)
itoa(n /10, s);
else{
i = 0;
if(n < 0)
s[i++] = '-';
}
s[i++] = abs(n) %10 + '0';
s[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 Programs Exercise 4-11.

K and R C, Solution to Exercise 4-11:
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 modify the K & R C Programs Exercise 4-3, Modify the gettop function so that it doesn’t need to use ungetch by using internal static variable. 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<ctype.h>
#define NUMBER '0'

int getch(void);

/* getop: get next operator or numeric operand. */
int getop(char s[])
{
int i ;
int c;
static int lastc = 0;

if(lastc == 0)
c = getch();
else{
c = lastc;
lastc = 0;
}

while((s[0] = c) == ' ' || c == 't')
c = getch();
s[1] = '';

if(!isdigit(c) && c != '.')
return c;
i = 0;
if(isdigit(c))
while(isdigit(s[++i] = c = getch()))
;
if(c == '.')

while(isdigit(s[++i] = c = getch()))
;

s[i] = '';
if(c != EOF)
lastc = c;
return NUMBER;

}
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

C Program to find sum and product using macros.

Write C Program to find sum and product using macros.
Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.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>
#define SUM(A, B) (A + B)
#define PROD(A, B) (A * B)
int main()
{
int num1, num2, sum, product;
printf("nEnter the Two numbersn");
scanf("%d%d",&num1,&num2);
sum=SUM(num1,num2);
product=PROD(num1,num2);
printf("nnSum of two numbers using Macros is:%dn",sum);
printf("nnProduct of two numbers using macros is:%dn",product);
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 Programs Exercise 4-10.

K and R C, Solution to Exercise 4-10:
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 modify the K & R C Programs Exercise 4-5, An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. revise the caluculater to use this aproach. 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<ctype.h>

#define MAXLINE 100
#define NUMBER '0' //SIGNAL THAT A NUMBER WAS FOUND
int getline(char line[], int limit);
int li = 0;
char line[MAXLINE];
/* Getop: get next operator or numeric operand. */
int Getop(char s[])
{
int i;
int c;

if(line[li] == '')
if(getline(line, MAXLINE) == 0)
return EOF;
else li = 0;
/* Skip whitespace */
while((s[0] = c = line[li++]) == ' ' || c == 't')
{
;
}
s[1] = '';

/* Not a number but may contain a unary minus. */
if(!isdigit(c) && c != '.' )
return c;
i = 0;

if(isdigit(c))
while(isdigit(s[++i] = c =line[i++]))
;

if(c == '.')
while(isdigit(s[++i] = c =line[i++]))
;
s[i] = '';
li--;
return NUMBER;
}

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 Programs Exercise 4-9.

K and R C, Solution to Exercise 4-9:
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 modify the K & R C Programs Exercise 4-8, getch and ungetch do not handlea pushed back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement the design
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>
#define BUFSIZE 100
int buf[BUFSIZE];
int bufp = 0;

//getch: get a (possibly pushed back) character
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
//ungetch: push a character back onto the input
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch: too many characters!");
else
buf[bufp++] = c;
}
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