strrchr()函数 在C++中,STRRCR()是一种预定义的用于字符串处理的函数。cstring是字符串函数所需的头文件。 此函数返回字符串中最后一个字符的指针。 最后一次需要查找的字符作为第二个参数传递给函数,而需要查找字符的字符串作为第一个参数传递给函数。 语法
null
char *strrchr(const char *str, int c)
这里,str是字符串,c是要定位的字符。它作为int升级传递,但在内部被转换回char。 应用 给定C++中的字符串,我们需要找到字符的最后一个出现,我们来说“A”。 例如:
Input : string = 'This is a string'Output :9Input :string = 'My name is Ayush'Output :12
算法 1.在strchr()函数中传递给定的字符串,并指出需要指向的字符。 2.函数返回一个值,打印该值。
CPP
// C++ program to demonstrate working strchr() #include <iostream> #include <cstring> using namespace std; int main() { char str[] = "This is a string" ; char * ch = strrchr (str, 'a' ); cout << ch - str + 1; return 0; } |
输出:
9
C例如:
C
// C code to demonstrate the working of // strrchr() #include <stdio.h> #include <string.h> // Driver function int main() { // initializing variables char st[] = "GeeksforGeeks" ; char ch = 'e' ; char * val; // Use of strrchr() // returns "ks" val = strrchr (st, ch); printf ( "String after last %c is : %s " , ch, val); char ch2 = 'm' ; // Use of strrchr() // returns null // test for null val = strrchr (st, ch2); printf ( "String after last %c is : %s " , ch2, val); return (0); } |
输出:
String after last e is : eks String after last m is : (null)
实际应用: 由于它在某个特定字符最后一次出现后返回整个字符串,因此可以使用它 提取字符串的后缀 例如,当我们知道第一个数字时,就知道一个面额中所有的前导零。这个例子如下所示。
C
// C code to demonstrate the application of // strrchr() #include <stdio.h> #include <string.h> // Driver function int main() { // initializing the denomination char denom[] = "Rs 10000000" ; // Printing original string printf ( "The original string is : %s" , denom); // initializing the initial number char first = '1' ; char * entire; // Use of strrchr() // returns entire number entire = strrchr (denom, first); printf ( "The denomination value is : %s " , entire); return (0); } |
输出:
The original string is : Rs 10000000The denomination value is : 10000000
本文由 阿尤什·萨克塞纳 和 维什纳维·特里帕蒂 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END