K & R C Programs Exercise 4-7.

K and R C, Solution to Exercise 4-7:
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 that will push back an entire string onto the the input.
ungets calls the routine ungetch len times, each time pushing back a character from the string s onto the input. ungets pushes the string back in reverse order.
The routine ungets does not need to worried about the buf and bufp. The routine ungetch handles buf, bufp, and error handling. 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>
/*ungets: push string back onto the input*/

void ungets(char s[])
{
int len=strlen(s);
void ungetch(int);
while (len >0)
ungetch(s[--len]);
}
int main(void)
{
char *s = "oh! this is for testing!";
int c;

ungets(s);
while ((c = getch()) != EOF)
putchar(c);
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-1.

K and R C, Solution to Exercise 4-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.
Write a C program which returns the position of the occurrence of sub string t in string s, or -1 if there is none.

/***********************************************************
* 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>
void main()
{
int flag=0;
char str[80],search[10];
puts("Enter a string:");
gets(str);

puts("Enter search substring:");
gets(search);
flag=strindex(str, search);
if (flag == -1)
{
printf("SEARCH UNSUCCESSFUL! AND POSITION IS:%d",flag);
}
else{
printf("SEARCH SUCCESSFUL! AND POSITION IS:%d",flag);
}
getch();
}

//strindex: returns the right most index of t in s, -1 if none*/
int strindex(char s[], char t[])
{
int k,i,j,pos;
pos = -1;
for(i=0;s[i] != ''; i++)
{
for(j=i, k = 0; t[k] != '' && s[j] == t[k]; j++, k++)
;
if (k > 0 && t[k] == '')
pos=i;
}
return pos;
}
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 2-5.

K and R C, Solution to Exercise 2-5:
 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.
Write the C 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.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
***********************************************************/

int any(char s1[], chars2[])
{
int i, j;
for(i=0;s1[i]!='';i++)

for(j=0;s2[[j]!='' ;j++)

if(s1[i]==s2[j])
return i;

return -1;
}
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!

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

K & R C Programs Exercise 2-4.

K and R C, Solution to Exercise 2-4:
 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.
Write an alternate version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.
Compare ,First string with the second string, if any character matched,  delete the matched character of the first string. 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
***********************************************************/


/*squeeze: delete each char in s1 which is in s2*/
void squeeze(char s1[], chars2[])
{
int i, j, k;
for(i=k=0;s1[i]!='';i++)
{
for(j=0;s2[[j]!='' && s2[j]!=s1[i];j++)
;
if(s2[j]=='')
s1[k++]=s1[i];
}
s1[k]='';
}
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!

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

K&R C Program Exercise 1-11

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. How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?. Read more about C Programming Language .

To test the word count program try some of the following inputs: 

1. input file contains zero words , output should be 0, 0, 0.
2. input file contains 1 enormous word without any newlines
3. input file contains all white space without newlines
4. input file contains 66000 newlines
5. input file contains word
{huge sequence of white space of different kinds}/word
6. input file contains 66000 single letter words,
66 to the line
7. input file contains 66000 words without any newlines


Some boundary conditions are:
-no input
-no words--just new lines
-no words--just blanks,tabs, and new lines
-one word per line--no blanks and tabs
-word starting at the beginning of the line
-word starting after some blanks.

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

K&R C Program Exercise 1-10

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. C Program to Replace each tabs by ‘t’, backspace by ‘b’, and each bakslash by ‘\’.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 BACK_SLASH '\'

int main(void)
{
int c;

while((c = getchar()) != EOF)
{
switch(c)
{
case 'b':
putchar(BACK_SLASH);
putchar('b');
break;
case 't':
putchar(BACK_SLASH);
putchar('t');
break;
case BACK_SLASH:
putchar(BACK_SLASH);
putchar(BACK_SLASH);
break;
default:
putchar(c);
break;
}
}
return 0;
}
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

K&R C Program Exercise 1-09

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. C Program to replacing each string of one or more blanks by a single blank.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>

int main(void)
{
int ch;
while ((ch = getchar()) != EOF) {
if (ch == ' ') {
putchar(ch);
while((ch = getchar()) == ' ' && ch != EOF)
;
}
if (ch == EOF)
break;

putchar(ch);
}
return 0;
}
Read more Similar C Programs
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!

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

C Program to search the perticulur pattern in the string using Horspool method.

C Program to find the substring in a String using the Horspool method. This algorithm uses the Brute-Forse method which searches the text between 0 and n-m, and after each cycle it shifts the pattern by one position to the right. 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>
#define MAX 500
int t[MAX];
void shifttable(char p[])
{
int i,j,m;
m=strlen(p);
for(i=0;i<MAX;i++)
t[i]=m;
for(j=0;j<m-1;j++)
t[p[j]]=m-1-j;
}
int horspool(char src[],char p[])
{
int i,j,k,m,n;
n=strlen(src);
m=strlen(p);
printf("nLength of text=%d",n);
printf("n Length of pattern=%d",m);
i=m-1;
while(i<n)
{
k=0;
while((k<m)&&(p[m-1-k]==src[i-k]))
k++;
if(k==m)
return(i-m+1);
else
i+=t[src[i]];
}
return -1;
}
void main()
{
char src[100],p[100];
int pos;
clrscr();
printf("Enter the text in which pattern is to be searched:n");
gets(src);
printf("Enter the pattern to be searched:n");
gets(p);
shifttable(p);
pos=horspool(src,p);
if(pos>=0)
printf("n The desired pattern was found starting from position %d",pos+1);
else
printf("n The pattern was not found in the given textn");
getch();
}
Read more Similar C Programs
Data Strucures

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!

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

C Program to count the number of occurrences of particular word in a string

C Program that counts the particular word in the given string. For example in the string ” the string is the set of charterers” , Number of occurrences of “the” is:2. 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()

{

 int strln,wordln,i,j,k,flag,count=0;

 char str[200],word[20];

 printf("Enter line of text:n");

 gets(str);

 printf("Enter the word to count:n");

 scanf("%s",word);

 strln=strlen(str);

 wordln=strlen(word);

 for(i=0;i<strln;i++)

 {

  if(str[i]==word[0]&&((str[i-1]==' '||i==0)&&(str[i+wordln]==' '||str[i+wordln]=='')))

  {

   for(flag=0,k=i+1,j=1;j<wordln;j++,k++)

   {

    if(str[k]==word[j])

    {

     flag++;

    }

   }

   if(flag==wordln-1)

   {

    count++;

   }

  }

 }

 printf("Number of occurence of '%s' = %dn",word,count);

}

Read more Similar C Programs
Searching in C

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

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

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

C Program to count number of characters in the file

C Program to count number of characters in the file. In this program you can learn c file operations. Here we counting the characters by reading the characters in the file one by one and if read character was not an ‘n’ ,’t’ or EOF, it increments the counter by one. 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>

void main()
{
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL)
{
printf("File can't be createda");
getch();
exit(0);
}
printf("Enter some text and press enter key:n");
while((ch=getche())!='r')
{
fputc(ch,fptr);
}
fclose(fptr);
fptr=fopen("text.txt","r");
printf("nContents of the File is:");
while((ch=fgetc(fptr))!=EOF)
{
count++;
printf("%c",ch);
}
fclose(fptr);
printf("nThe number of characters present in file is: %d",count);
getch();
}

Read more Similar 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!

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