📜  C++中的strchr()函数及其应用

📅  最后修改于: 2021-05-30 11:21:13             🧑  作者: Mango

在C++中,和strchr()是用于在一个字符串查找字符的出现的预定义的函数。它存在于cstring头文件中。

句法

// Returns pointer to the first occurrence
// of c in str[]
char *strchr(const char *str, int c) 

请注意,将c作为其int促销传递,但在内部将其视为char。
应用
给定C++中的字符串,我们需要找到一个字符的第一个匹配项,让我们说“ a”。

例子:

Input : str[] = 'This is a string'
Output : 9

Input : str[] = 'My name is Ayush'
Output : 5

算法
1.在strchr()函数传递给定的字符串,并提及您需要指向的字符。
2.函数返回一个值,打印该值。

下面是上述算法的实现:

CPP
// CPP program to find position of a character
// in a given string.
#include 
#include 
using namespace std;
 
int main()
{
    char str[] = "My name is Ayush";
    char* ch = strchr(str, 'a');
    cout << ch - str + 1;
    return 0;
}


CPP
// CPP program to demonstrate working of strchr()
#include 
#include 
using namespace std;
// Driver code
int main()
{
    char str[] = "My name is Ayush";
    char ch = 'A', ch2 = 'z';
    if (strchr(str, ch) != NULL)
        cout << ch << " "
             << "is present in string" << endl;
    else
        cout << ch << " "
             << "is not present in string" << endl;
    if (strchr(str, ch2) != NULL)
        cout << ch2 << " "
             << "is present in string" << endl;
    else
        cout << ch2 << " "
             << "is not present in string" << endl;
    return 0;
}


C
// C program to find directory path
#include 
#include
   
int main()
{
   char string[]={"/home/test/sample"};
   int len;
     
    //position of last char
    char* pos;
 
    // save length of string
    len = strlen(string);
 
    // Find the last character with
    pos = strchr(string,'/') ;
    printf("%s\n",string);
 
    // replace last occurrence of / with NULL character.
    *pos='\0';                 
    printf("%s\n",string);
 
    return 0;
}


输出
5

strchr()函数还可用于检查strinG中字符的存在。输入包含要检查的字符(如果它存在于字符串) 。
例如–让我们检查字符串是否存在字符A和z –“我的名字是Ayush”

Input : str[] = 'My name is Ayush', 
        ch1 = 'A', ch2 = 'z'
Output : A is present in the string
         z is not present in the string

算法
1.在给定的字符串中将字符作为第二个参数传递到strchr()函数,并检查返回的值是否不为null
2.如果函数返回NULL值,则意味着该字符串不包含字符,因此,打印所需的语句。
3.否则,如果函数未返回NULL值,则意味着字符串包含字符,因此,打印所需的语句。

下面是上述算法的实现:

CPP

// CPP program to demonstrate working of strchr()
#include 
#include 
using namespace std;
// Driver code
int main()
{
    char str[] = "My name is Ayush";
    char ch = 'A', ch2 = 'z';
    if (strchr(str, ch) != NULL)
        cout << ch << " "
             << "is present in string" << endl;
    else
        cout << ch << " "
             << "is not present in string" << endl;
    if (strchr(str, ch2) != NULL)
        cout << ch2 << " "
             << "is present in string" << endl;
    else
        cout << ch2 << " "
             << "is not present in string" << endl;
    return 0;
}
输出:
A is present in string
z is not present in string

strchr()函数可用于查找Linux的绝对目录路径:(由Ekta_nehwal提供)

例子:

Input : /home/test/sample
Output : /home/test

算法:

  1. 通过使用strrchr查找目录路径中最后一个“ /”的位置。
  2. 将出现的内容替换为NULL字符。

下面是上述算法的实现:

C

// C program to find directory path
#include 
#include
   
int main()
{
   char string[]={"/home/test/sample"};
   int len;
     
    //position of last char
    char* pos;
 
    // save length of string
    len = strlen(string);
 
    // Find the last character with
    pos = strchr(string,'/') ;
    printf("%s\n",string);
 
    // replace last occurrence of / with NULL character.
    *pos='\0';                 
    printf("%s\n",string);
 
    return 0;
}
输出
/home/test/sample
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”