daxelrod
4
Q:

strchr function in c++

// CPP program to demonstrate working of strchr() 
#include <iostream> 
#include <cstring> 
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; 
} 
0
#include <iostream>
#include <string.h>

using namespace std;
int main() {
   char str1[] = "Apples are red";
   char str2[] = "are";
   char *ptr;
   ptr = strstr(str1, str2);

   if(ptr)
   cout<<"Occurance of \""<< str2 <<"\" in \""<< str1 <<"\" is at position "<<ptr - str1 + 1;

   else
   cout<<"There is no occurance of \""<< str2 <<"\" in "<<str1;
   return 0;
}
0
/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}
0
// CPP program to illustrate strstr() 
#include <string.h> 
#include <stdio.h> 
  
int main() 
{ 
    // Take any two strings 
    char s1[] = "Fun with STL"; 
    char s2[] = "STL"; 
    char* p; 
  
    // Find first occurrence of s2 in s1 
    p = strstr(s1, s2); 
  
    // Prints the result 
    if (p) { 
        strcpy(p, "Strings"); 
        printf("%s", s1); 
    } else
        printf("String not found\n"); 
  
    return 0; 
} 
0

New to Communities?

Join the community