How to i check a string to see if another string is inside it.
eg.
string1: "folder1\folder2\folder3\mytext.txt
string2: "mytext.txt"
How do see if string2 is present in string 1
thankyou
Printable View
How to i check a string to see if another string is inside it.
eg.
string1: "folder1\folder2\folder3\mytext.txt
string2: "mytext.txt"
How do see if string2 is present in string 1
thankyou
I assume that you want to use this for folders and files. Some sort of split, maybe? If this is it then you should use command "fnsplit". :D
char *str1 = "folder1\folder2\folder3\mytext.txt", *str2 = "mytext.txt";
char *buffer = strstr( str1, str2 );
if( !strcmp( buffer, str2 ) )
puts( "string occurs :-)" );
didn't compile it, but should work. note that we can compare buffer and str2 only, if the searched string is in the end of str1.
Close but no cigar. :) I'd prefer...Quote:
Originally posted by Unregistered
char *str1 = "folder1\folder2\folder3\mytext.txt", *str2 = "mytext.txt";
char *buffer = strstr( str1, str2 );
if( !strcmp( buffer, str2 ) )
puts( "string occurs :-)" );
didn't compile it, but should work. note that we can compare buffer and str2 only, if the searched string is in the end of str1.
Ian WoodsCode:char * buf= strstr(str1,str2);
if (buf) {
/* it's in there */
} else {
/* it isn't */
}
The situation is that, i will be passing a command line argument into the program (using argv[]) from unix and if the word 'encode' is passed into it then it will perform an encode function ive done or if the word decode is passed into the program it will do a decode function.
cheers
What about this:
:cool:Code:if (strcmp(argv[1], "-encode") == 0)
{
// your code
}
This is a different situation to your original post. Assuming you want your command line something like this...Quote:
Originally posted by jackwoz
The situation is that, i will be passing a command line argument into the program (using argv[]) from unix and if the word 'encode' is passed into it then it will perform an encode function ive done or if the word decode is passed into the program it will do a decode function.
cheers
myprog encode myfile
then you can just strcmp with argv[1] (since argv has been split based on the white space). So, you could imagine something like this:
Ian WoodsCode:int main(int argc, char ** argv) {
if (argc!=3) {
/* show help message */
return EXIT_FAILURE;
}
if (!strcmp(argv[1],"encode")) {
/* do some encoding */
} else if (!strcmp(argv[1],"decode")) {
/* do some decoding */
} else {
/* it was neither encode or decode, so make an error message */
}
...
Cheers Ian.I seemed to have got things confused in my head.