-
strings?
i have this program i have been working on and im at the point wher i dont know what string compare or type of string to use or compose it into my code.
-----------------------------------------------------------------------------------
the program should prompt the user to input two character strings(example the imput may say "goodbye" and "bye")...
the program should have a function that searches the first character string for the second string and return the location of the string....
in the example if the user enters "goodbye" and "bye" the return value from the program would be 5.
-----------------------------------------------------------------------------------
i cant get the 5 and im not using the right string pattern in my code if you could help me out in any way i would appreciate it...
thanks
-
What i think you're looking for is strchr().
Looking it up in my help files i come across this handy little example:
Code:
/* STRCHR.C: This program illustrates searching for a character
* with strchr (search forward) or strrchr (search backward).
*/
#include <string.h>
#include <stdio.h>
int ch = 'r';
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";
void main( void )
{
char *pdest;
int result;
printf( "String to be searched: \n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
printf( "Search char:\t%c\n", ch );
/* Search forward. */
pdest = strchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tfirst %c found at position %d\n\n",
ch, result );
else
printf( "Result:\t%c not found\n" );
/* Search backward. */
pdest = strrchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tlast %c found at position %d\n\n", ch, result );
else
printf( "Result:\t%c not found\n" );
}
Enjoy.
-
looking at the example it looks like strchr only finds the instance(s) of a character and not a string. i think The WaRped OnE wanted to search a string for a series of characters, like i've done below. but hey, i could be wrong.
Code:
#include<iostream.h>
#include<string.h>
int main()
{
char String_To_Search[256],
String_To_Find[256];
int iFound = 1;
int iPos = 0,
x = 0,
iFound_Pos,
Found_Char,
Search_Char;
cout<<"Enter a string to search\n";
cin.getline(String_To_Search, 255, '\n');
cout<<"\n";
cout<<"Enter a string to search for.\n";
cin.getline(String_To_Find, 255, '\n');
cout<<"\n";
if(strstr(String_To_Search, String_To_Find))
{
do
{
Search_Char = String_To_Find[x];
Found_Char = String_To_Search[iPos];
iPos++;
iFound_Pos = iPos;
if(Search_Char == Found_Char)
{
x++;
if(x == (int)strlen(String_To_Find))
{
cout<<String_To_Find<<" found at position ";
cout<<iFound_Pos<<".";
iFound = 0;
}
}
else
{
x = 0;
}
}
while(iFound != 0);
}
else
{
cout<<String_To_Find<<" not found";
}
return 0;
}
-
I dont think so
I dont think you understand, the word "bye" is not a charachter, it is a whole string... a charachter would be 'b'.
SPH
-
-
i
-
It should be exactly what hes looking for...
char *strstr( const char *string, const char *strCharSet );
Return Value
Each of these functions returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.
Parameters
string
Null-terminated string to search
strCharSet
Null-terminated string to search for