comparing string with character

P

Pradyut

I have made a program in which I'm trying to return the string in
which the character is found.

I'm doing this through pointers. I'm not able to figure out the
correct way:



printf("%s", *string);

while(string != '\0')

{

printf("this is a test");

s = *string;

if(s == w)

{

printf("%c", s);

return (s1);

break;

}

*string++;





Please can some body help me



This is my code : -



#include <stdio.h>

#include <conio.h>

char *xstrchr(char *, char);

void main()

{

char a ='t';

//printf("%c", a);

char b[][8] = {"Scope", "Rule", "Now", "Testing"};



int i;

for(i=0; i<=3; i++)

{

char *p = xstrchr(b, a);

//printf("%s", b);

printf("%s", p);

}

/*i=0;



while (b[3]!='\0')

{

if (a == b[3])

printf("\nThis is a test: %s",
b);

i++;

}*/

}

char *xstrchr(char *string, char w)

{

//while(*string!='\0')

//{

// if (*string == w)

// {

// //printf("%s", string);

// return *string;

// break;

// }

// //printf("%s", string);

// string++;

//}

//char *s1 = string;



printf("%s", *string);

while(string != '\0')

{

printf("this is a test");

if(s == w)

{

printf("%c", s);

return (s1);

break;

}

*string++;

}

printf("\n");

return NULL;

}





Thanks



Pradyut
http://pradyut.tk
http://groups.yahoo.com/group/d_dom/
http://groups-beta.google.com/group/oop_programming
India
 
U

ulrich

I have made a program in which I'm trying to return the string in
which the character is found.

#include<string>
std::string s = "teststring";
s.c_str(); //this returns a char*, a pointer to a zero terminated
sequence of charcters ("C-style string")

to check if a string contains a character, character-sequence, another
string, use the
std::string::find...() methods. a return value != std::string::npos
indicates having found what was searched for.
 
G

George Faraj

Eww, C.

char a = 'e';
std::string s("hello");
std::string::iterator found = std::find(s.begin(), s.end(), a);

Now that wasn't so hard, was it?

George Faraj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top