Advertisement
char *strchr(const char *str, int c)
// Program to find a character in a String and return remaining string after it.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char arr[] = "www.decodejava.com";
char ch = 'j';
char *p = strchr(arr,ch); //it prints the string after the searched char value
cout<<"String to be searched for a value : "<< arr << "\n";
cout<<"char value to be searched in String : " << ch << "\n";
cout<<"Result of search : "<< p;
return 0;
}
String to be searched for a value : www.decodejava.com
char value to be searched in String : j
Result of search : java.com
// Program to find a character at a particular index of a String.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char arr[] = "welcome to this site";
char ch = 'k';
char *p = strchr(arr,ch);
cout<< "String to be searched for a value : "<< arr << "\n";
cout<< "char value to be searched in String : " << ch << "\n";
cout<< "Result of search : " << p;
return 0;
}
String to be searched for a value : welcome to this site
char value to be searched in String : k
Result of search :
Advertisement
// Program to find a character in a string, with multiple occurrences
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char arr[] = "a beautiful sky";
char ch = 'e';
char *p = strchr(arr,ch); // it prints the string after the searched char value
cout<< "String to be searched for a value : " << arr << "\n";
cout<< "char value to be searched in String : " << ch << "\n";
cout<< "Result of search : " << p;
return 0;
}
String to be searched for a value : a beautiful sky
char value to be searched in String : e
Result of search : eautiful sky
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement