當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 strchr()用法及代碼示例

本節將討論 C 編程語言中字符串頭的 strchr() 函數。 strchr() 函數用於在原始字符串中查找指定字符的第一次出現。換句話說,strchr() 函數檢查原始字符串是否包含定義的字符。如果在字符串中找到該字符,則返回一個指針值;否則,它返回一個空指針。在C語言中使用strchr()函數時,需要在程序中導入<string.h>頭文件。

strchr() function in C

用法

char *strchr (const char *str, int c);

在上麵的語法中,一個 strchr() 函數有兩個參數:str 和 ch。

str:str 表示要在其中搜索字符的原始字符串。

ch:ch 是字符類型變量,表示在字符串 str 中搜索的字符。

返回值:它返回一個指針值,其中包含給定字符串中第一次出現的字符。

演示 strchr() 函數使用的程序

讓我們考慮一個例子來檢查給定字符串中字符的出現。

程序1.c

#include <stdio.h>
#include <string.h>
int main ()
{
    const char str[] = "Use strchr() function in C.";
    const char ch = 's'; // it is searched in str[] array
    char *ptr; // declare character pointer ptr
    
    printf (" Original string is:%s \n", str);
    // use strchr() function and pass str in which ch is to be searched
    ptr = strchr( str, ch);
    printf (" The first occurrence of the '%c' in '%s' string  is:'%s' ", ch, str, ptr);
    return 0;
}

輸出

Original string is:Use strchr() function in C.
 The first occurrence of the 's' in 'Use strchr() function in C.' string is:'e strchr() function in C.'

在上麵的程序中,我們給strchr()函數傳遞了一個str參數來搜索字符's',當找到一個字符時,返回一個指針ptr值。 ptr 變量包含來自原始字符串的指定字符的值,直到 ch 變量在字符串中沒有獲得空字符。

使用 strchr() 函數和 if-else 語句搜索字符的程序

讓我們考慮使用 strchr() 函數和 if-else 語句在 C 編程語言的給定字符串中獲取第一個字符的出現的示例。

程序1.c

#include <stdio.h>
#include <string.h> // use string.h header file 

int main ()
{
    
    const char *s = "javatpoint"; // initialization of the constant char pointer
    char ch; // declare a ch variable
    
    printf (" Original string:\"%s\" \n ", s); // print the string
    
    // take a character from the user 
    printf ("Please enter a character you want to search in the string:");
    scanf (" %c", &ch;);
    
    // it checks whether the specified character exists in the string
    if ( strchr (s, ch) != NULL )
    {
        printf (" \n '%c' is found in \"%s\" ", ch, s);
        }
    // if the character is not found, the below statement is executed		
    else
        printf (" \n '%c' is not found in \"%s\" ", ch, s);
    
    return 0;	
}

輸出

Original string:"javatpoint"
 Please enter a character you want to search in the string:p

 'p' is found in "javatpoint"

2nd執行:

Original string:"javatpoint"
 Please enter a character you want to search in the string:b

 'b' is not found in "javatpoint"

在上麵的程序中,我們傳遞了一個字符串 "javatpoint" 來搜索指定的字符。這裏我們將 'p' 字符作為用戶的輸入並在字符串中進行搜索。之後,if 語句使用 strchr() 函數檢查字符是否出現,如果存在則打印指定的字符。否則,表示在字符串中找不到該字符。

獲取給定字符串中每個字符出現的程序

讓我們考慮一個示例,使用 strchr() 函數和 C 編程語言的 while 循環打印給定字符串中每個字符的出現次數。

程序3.c

#include <stdio.h>
#include <string.h>
int main ()
{
    // initialize a string
    char str[] = " Welcome to the JavaTpoint site";
    char *ptr; // declare a pointer variable
    int i = 1; // declare and initialize i
    
    // use strchr() function to check the occurrence of the character 
    ptr = strchr (str, 'e' );
    
    // use while loop to checks ptr until it becomes null
    while (ptr != NULL)
    {
        printf (" Given character 'e' found at position %d \n", (ptr - str + 1));
        printf (" Occurrence of the character 'e':%d \n", i);
        printf (" The occurrence of the character 'e' in the string \"%s\" is \"%s\" \n \n", str, ptr);
        
        // use strchr() function to update the position of the string
        ptr = strchr (ptr + 1, 'e');
        i++;
    }
    return 0;
}

輸出

Given character 'e' found at position 3
 Occurrence of the character 'e':1
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "elcome to the JavaTpoint site"

 Given character 'e' found at position 8	
 Occurrence of the character 'e':2
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "e to the JavaTpoint site"

 Given character 'e' found at position 15
 Occurrence of the character 'e':3
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "e JavaTpoint site"

 Given character 'e' found at position 31
 Occurrence of the character 'e':4
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "e"




相關用法


注:本文由純淨天空篩選整理自 strchr() function in C。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。