script problem

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
char str[]="this is some text";
if(strchr(str,'is'))
{
cout << "true";
}
}
it works fine but if
if instead of 'is' in parameter 2 of strchr i use a variable it gives errors
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* strchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}


http://www.cplusplus.com/reference/clibrary/cstring/strchr/
no i dont want this i want a variable
What error are you getting?
strchr() takes a single character for its second argument. What type of variable did you define? If you want to look for a string within a string, you can try strstr().
Since you're using C++, try std::string and it's respective functions. They are easier to use.
Topic archived. No new replies allowed.