Jan 26, 2012

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 info@c-program-example.com
* 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++ = '\0';
}

/*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 == '\0' || --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

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

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 info@c-program-example.com
* 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 != '\0'; s++)
 {
  n++;
 }
 return n;
}

int str_cmp(char *s, char *t)
{
 for(;*s == *t; s++, t++)
  if(*s == '\0')
   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 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

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

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 info@c-program-example.com
* 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 '\0' */ 
 while('\0' != *s)
 {

  ++s;
 }

 /* now copy until we run out of string to copy */
 while('\0' != (*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 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

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

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 info@c-program-example.com
* 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: %f\n", 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

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