Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

memchr in C language

by suresh

The C memchr function is a String Function, which will find the first occurrence of the character, and returns the pointer to it. This function uses its third argument to restrict the search.

The basic syntax of the memchr in C Programming language is as shown below.

void *memchr(const void *str, int c, size_t n);
  • str: A valid string
  • c: The value that you want to search inside str
  • n: The number of characters that you want to search within the search object str.

memchr in C Language Example

The memchr function used to search within the user-specified string. This program will help you to understand the memchr with multiple examples.

TIP: You have to include the #include<string.h> header before using this memchr String Function.

/* memchr in C Programming Example */
 
#include <stdio.h> 
#include<string.h>

int main()
{  
   	char str[] = "C Programming Language";
   	char ch = 'L';
   	char *result;
   	char *result2;
   	
   	result = memchr(str, ch, strlen(str));
	
   	printf("\n The Final String From %c = %s", ch, result);
   	
   	result2 = memchr("Tutorial Gateway", 'G', strlen(str));
   	
   	printf("\n The Final String From %c = %s", ch, result2);
   	
   	return 0;
}

OUTPUT

memchr in C Language 1

memchr in C Example 2

This program allows the user to enter his/her string and the character that they want to look at.

Next, it is going to use the memchr function in C Programming to return the part of a string that starts from using the specified character.

/* memchr in C Programming Example */
 
#include <stdio.h> 
#include<string.h>

int main()
{  
   	char str[40];
   	char ch;
   	char *result;
   	
   	printf("\n Please Enter any String  : ");
	gets(str);	
	
	printf("\n Please Enter any Charcater that you want to search for  : ");
	scanf("%c", &ch);
		
   	result = memchr(str, ch, strlen(str));
	
   	printf("\n The Final String From %c = %s", ch, result);
   	
	return 0;
   	
}

OUTPUT

memchr in C Language 2

This time we will look for non-existing character

memchr in C Language 3

Although the given character exists in a string, it is returning NULL. It is because we restricted the search to the first 5 characters, and G does not exist in the first five characters.

memchr in C Language 4

Placed Under: C Programming

  • C Comments
  • C Escape Sequence
  • C Programming Operators
  • C Arithmetic Operators
  • C Assignment Operators
  • C Bitwise Operators
  • C Conditional Operator
  • C Increment & Decrement Optr
  • C Logical Operators
  • C Relational Operators
  • C Sizeof Operator
  • C If Statement
  • C IF ELSE Statement
  • C Else If Statement
  • C Nested If
  • C Break Statement
  • C Continue Statement
  • C Goto Statement
  • C Switch Case
  • C For Loop
  • C While Loop
  • C Do While Loop
  • C Arrays
  • C Two Dimensional Array
  • C Multi Dimensional Array
  • C String
  • C Structures
  • C Nested Structures
  • C Array of Structures
  • C Structures and Functions
  • C Union
  • C Structure & Union differences
  • C Pointers
  • C Pass Pointers to Functions
  • C GETS
  • C fgets Function
  • C fgetc
  • C fputs function
  • C fputc
  • C Functions
  • C Types of Functions
  • C Pass Array to Functions
  • Call By Value & Call By Reference
  • C Recursion
  • C Program Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy