Friday, November 14, 2014

Memchr() & Memicmp() Memmove Memcpy() OF C Programs



Program for memcpy() function in C
 
#include 
#include
#include 
int main()
{
   char str1[10] = "fresh";
   char str2[10] ;
   if (memcpy(str2,str1, strlen(str1)))
   {
      printf("Elements in str1 are copied to str2 .\n");
      printf("str1 = %s\n str2 = %s \n", str1, str2);
   }
   else
     printf("Error while coping str1 into str2.\n");
   return 0;
}

Output

Elements in str1 are copied to str2.
str1 = fresh
str2 = fresh

Program for memmove() function in C
 
#include 
#include 
int main()
{
  char str1[10] = "fresh";
    printf("str1 before memmove\n");
    printf("str1 = %s\n ", str1);
  if (memmove(str1+2,str1, strlen(str1)))
  {
    printf("Elements in str1 are moved/overlapped on str1.\n");
    printf("str1 = %s \n", str1);
  }
  else
    printf("Error while coping str1 into str2.\n");
  return 0;
}

 Output

str1 before memmove
str1 = fresh
Elements in str1 are moved/overlapped on str1.
str1 = frfresh

Program for memcmp () function in C

#include 
#include 
int main()
{
  char str1[10] = "fresh";
  char str2[10] = "refresh";
  if (!memcmp(str1,str2, 5*sizeof(char)))
    printf("Elements in str1 and str2 are same.\n");
  else
    printf("Elements in str1 and str2 are not same.\n");
  return 0;
}

 Output

Elements in str1 and str2 are not same.

Program for memicmp () function in C

#include 
#include 
int main()
{
  char str1[10] = "fresh";
  char str2[10] = "fresh";
  if (!memicmp(str1,str2, 5*sizeof(char)))
    printf("Elements in str1 and str2 are same.\n");
  else
    printf("Elements in str1 and str2 are not same.\n");
  return 0;
}

 Output

Elements in str1 and str2 are same. Elements in str1 and str2 are same.

Program for memchr() function in C

#include 
#include
#include 
int main ()
{
  char *ptr;
  char string[] = "fresh2refresh";
  ptr = (char *) memchr (string, 'h', strlen(string));
  if (ptr != NULL)
    printf ("character 'h' is found at " \
            "position %d.\n", ptr-string+1);
  else
    printf ("character 'h' is not found.\n");
  return 0;
}

Output

character ‘h’ is found at position 5.

No comments:

Post a Comment