K & R C Programs Exercise 7-9.

K and R C, Solution to Exercise 7-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.
Write a C Functions like isupper() can be implemented to save space or to save time. Explore both possibilities. Read more about C Programming Language .
isupper: return 1 (true) if c is an upper case letter
Normal C function:

int  isupper(char c)
{
if (c >= 'A' && c <= 'Z')
return 1;
else
return 0;
}

This simple code tests the character is upper or lower , If the character within the range of ASCII upper case letters it returns 1, otherwise 0.
To save space or to save time using the macros is the best possibility
C Code:

#define isupper(c)  ((c) > = 'A' && (c) <= 'Z') ? 1:0

Macro version of isupper is more efficient because, there is no overhead of the function call and it uses more space because the macro is expanded in line every time it is invoked.

Read more 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 6-2.

K and R C, Solution to Exercise 6-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.
Write a C program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don’t count words within strings and comments. Make 6 a parameter that can be from the command line. 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>
#include<string.h>
#include<stdlib.h>

struct tnode {
char *word;
int match;
struct tnode *left;
struct tnode *right;
};
#define MAXWORD
#define YES 0
#define NO 0

struct tnode *addtreex(struct tnode *, char *,int, int);
void treexprint(struct tnode *);
int getword(char *,int);


main(int argc, char argv[])
{
struct tnode *root;
char word[MAXWORD];
int found = NO;
int num;

num = (--argc && (*++argv)[0] == '-') ? atoi(argv[0] +1) : 6;
root = NULL;
while(getword(word, MAXWORD) != EOF) {
if(isalpha(word[0]) && strlen(word) >= num)
root = addtreex(root, word, num, &found);
found = NO;
}
treexprint(root);
return 0;
}
struct tnode *talloc(void);
int compare(char *,struct tnode *, int, int *);
//addtreex: add a node with w, at or below p
struct tnode *addtreex(struct tnode *p, char *w,int num, int *found)
{
int cond;
if(p == NULL) {
p = talloc();
p->word = strdup(w);
p->match = *found;
p->left = p->right = NULL;
}
else if((cond = compare(w,p,num,found)) < 0)
p->left = addtreex(p->left, w, num, found);
else if(cond > 0)
p->right = addtreex(p->right, w, num, found);
return p;
}

//compare:compare words and update p->match
int compare(char *s,struct tnode *p, int num, int *found)
{
int i;
char *t = p->word;
for(i = 0; *s == *t;i++,s++,t++)
if(*s == '')
return 0;
if(i >= num) {
*found = YES;
p->match = YES;
}
return *s - *t;
}

/*treexprint: in-order print of tree p if p->match == YES */
void treexprint(struct tnode *p)
{
if(p != NULL) {
treexprint(p->left);
if(p->match)
printf("%sn",p->word);
treexprint(p->right);
}
}


//getword: get next word or character from input
int getword(char *word, int lim)
{
int c, d, comment(void), getch(void);
void ungetch(int);
char *w = word;
while(isspace(c = getch()))
;
if(c !=EOF)
*w++ = c;
if(isalpha(c) || c == '-' || c == '#'){
for(; --lim > 0; w++)
if(!isalnum(*w = getch()) && *w != '-') {
ungetch(*w);
break;
}
}
else if(c == ''' || c == '\'){
for(; --lim > 0; w++)
if(((*w = getch()) == '\')
*++w = getch();
else if(*w == c) {
w++;
break;
}else if(*w == EOF)
break;
}else if(c == '/')
if((d = getch()) == '*')
c = comment();
else
ungetch(d);
*w = '';
return c;
}

Read more 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 show examples of the strtol function.

C program To show examples of the strtol function.
strtol() function converts the string to the long integer, and strtol() can accept the number in various bases, and converts it into the decimal number or base. 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 <stdlib.h>

main()
{
char num[10];

/* Test a valid number */
strcpy(num,"13");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));

puts("----------------------------------");

/* Test a slightly valid number
* Returns the same results as
* above. */
strcpy(num, "13hzcd");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));

puts("----------------------------------");

/* Test an invalid number
* Returns ZERO */
strcpy(num, "hzcd");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));


puts("----------------------------------");

/* Test 0 base.
* This will look at the number
* and decide the base for its self!
*/
strcpy(num, "13");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

strcpy(num, "013");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

strcpy(num, "0x13");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 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 the strtok function.

C Program to demonstrate the strtok function.
strtok function breaks pointed by the string1 into sequence of tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.
In this program we split the test_string, i,e “string to split up ” to
string
to
split
up
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>

main()
{

char test_string[50]="string to split up";



char *sub_string;

/* Extract first string */
printf("%sn", strtok(test_string, " "));

/* Extract remaining
* strings */
while ( (sub_string=strtok(NULL, " ")) != NULL)
{
printf("%sn", sub_string);
}
}
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 the strstr function.

C Program to demonstrate the strstr function.
strstr function finds the substring in a string. strstr() returns pointer of the first occurrence of the string other wise it returns the null pointer is returned.
In this program strstr returns a pointer into ‘string’ if ‘test’ is found, if not found, NULL is returned. 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>

main()
{
char string[]="string to search";
char test[]="sear";
if (strstr(string, test)) puts("String found");

}
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 strpbrk function.

Write C programt to demonstrate strpbrk function.
strpbrk function locate the first occurrence pointed by the of string s1 from the string pointed to by s2. In this program, We Turns miscellaneous field separators into just a space separating tokens for easy parsing by SSCANF. Eventually, the character separators and replacement character will be passed in as strings. 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 <strings.h>

#define LINE_BUF 100

void find_comment(char *);

main()
{
char line[LINE_BUF];
char *sep;
int var1, var2;

while (fgets(line, LINE_BUF, stdin)) {

/*
* Check this out: Since SEP is a pointer to type char, when line is
* assigned to sep, really the first address is assigned to sep. LINE
* is the address of the start of the string. In contrast, LINE[0]
* is the first character of the string.
*/

sep = line;

while (sep != 0) {
sep = strpbrk(line, ";.&:,");
if (sep != 0)
*sep = ' ';
}
fputs(line, stdout);
}
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

K & R C Programs Exercise 5-5.

K and R C, Solution to Exercise 5-5:
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 write the versions of the library functions strncpy, strncat, and strncmp, which operate on at most the first n characters of their argument strings.
For example, strncpy(s, t, n) copies at most n characters of t to s. 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
***********************************************************/

/*strncpy: copy n characters from t to s*/
void strncpy(char *s, char *t, int n)
{
while(*t && n--> 0)
*s++ = *t++;
while( n--> 0)
*s++ = '';
}

/*strncat: concatenate n characters of t to end of s*/
void strncat(char *s, char *t, int n)
{
void strncpy(char *s, char *t, int n);
int strlen(char *);
strncpy(s+strlen(s), t, n);
}

/*strncmp: comapre at most n characters of t with s */
int strncmp(char *s, char *t, int n)
{
for( ; *s == *t; s++, t++)
if(*s == '' || --n <= 0)
{
return 0;
}
return *s - *t;
}
Read more 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-4.

K and R C, Solution to Exercise 5-4:
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, that returns 1 if the string t occurs at the end of the string s, and zero otherwise. 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>

//finds the string length, standard "strlen" function
int str_len(char *s)
{
int n;

for(n = 0; *s != ''; s++)
{
n++;
}
return n;
}

int str_cmp(char *s, char *t)
{
for(;*s == *t; s++, t++)
if(*s == '')
return 0;
return *s - *t;
}


int str_end(char *s, char *t)
{
int Result = 0;
int s_length = 0;
int t_length = 0;

/* get the lengths of the strings */
s_length = str_len(s);
t_length = str_len(t);

/* check if the lengths mean that the string t could fit at the string s */
if(t_length <= s_length)
{
/* advance the s pointer to where the string t would have to start in string s */
s += s_length - t_length;

/* and make the compare using strcmp */
if(0 == str_cmp(s, t))
{
Result = 1;
}
}

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

if(str_end(Str1, Str2))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str2);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str2);
}
if(str_end(Str1, Str3))
{
printf("The string (%s) has (%s) at the end.n", Str1, Str3);
}
else
{
printf("The string (%s) doesn't have (%s) at the end.n", Str1, Str3);
}



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