Design a function to search given character in given string in c

26 Sep 2022 Balmiki Mandal 0 C Programming

Searching for a Character in a String in C: Function Design

"Design a function to search a given character in a given string" means creating a function that takes a string and a character as input, and then searches for the given character within the string. The function should return the index of the first occurrence of the character in the string, or -1 if the character is not found in the string.

To search for a character in a string, you can use a loop to iterate over each character in the string, comparing it to the target character. If the target character is found, you can return its index. If the end of the string is reached without finding the character, you can return -1 to indicate that the character was not found.

We can use the following steps:

Define the function prototype:

C Programming
char* search_character(const char* string, char character);

This function takes a string and a character as input and returns a pointer to the first occurrence of the character in the string, or NULL if the character is not found.

Implement the function body:

C Programming
char* search_character(const char* string, char character) {
  // Iterate through the string until we find the character or reach the end of the string.
  for (int i = 0; string[i] != '\0'; i++) {
    if (string[i] == character) {
      return &string[i];
    }
  }

  // If we reach the end of the string without finding the character, return NULL.
  return NULL;
}

This function works by iterating through the string until it finds the character or reaches the end of the string. If the character is found, the function returns a pointer to the character. Otherwise, the function returns NULL.

Test the function:

C Programming
#include <stdio.h>

int main() {
  char string[] = "Hello, world!";
  char character = 'l';

  // Search for the character in the string.
  char* found_character = search_character(string, character);

  // If the character was found, print its index. Otherwise, print a message.
  if (found_character != NULL) {
    printf("The character '%c' was found at index %d.\n", character, found_character - string);
  } else {
    printf("The character '%c' was not found in the string.\n", character);
  }

  return 0;
}

Output:

The character 'l' was found at index 2.

This is just a simple example of a function to search a given character in a given string in C. You can modify the function to meet your specific needs, such as searching for multiple characters or searching for a character within a substring.

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

design a function to reverse data in between two location

Design a function to find string length in c

Design a Function to print the string character by character

Design a function to print the elements of a given integers array.

Design a function for bubble sort in c

Design a function to swapping of two number

Design a function to check the given number is Armstrong or not if Armstrong return 1 else 0

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.