strstr returns a pointer to the found character, so you could use pointer arithmetic: (Note: this code not tested for its ability to compile, it's one step away from pseudocode.)
Copychar * source = "test string"; /* assume source address is */
/* 0x10 for example */
char * found = strstr( source, "in" ); /* should return 0x18 */
if (found != NULL) /* strstr returns NULL if item not found */
{
int index = found - source; /* index is 8 */
/* source[8] gets you "i" */
}
Answer from Bill on Stack Overflowstrstr returns a pointer to the found character, so you could use pointer arithmetic: (Note: this code not tested for its ability to compile, it's one step away from pseudocode.)
Copychar * source = "test string"; /* assume source address is */
/* 0x10 for example */
char * found = strstr( source, "in" ); /* should return 0x18 */
if (found != NULL) /* strstr returns NULL if item not found */
{
int index = found - source; /* index is 8 */
/* source[8] gets you "i" */
}
I think that
size_t strcspn ( const char * str1, const char * str2 );
is what you want. Here is an example pulled from here:
Copy/* strcspn example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "fcba73";
char keys[] = "1234567890";
int i;
i = strcspn (str,keys);
printf ("The first number in str is at position %d.\n",i+1);
return 0;
}
Videos
I'm new to C#. What is wrong with these lines of code?
if (inventory.Contains(input.Substring(4, input.Length - 5)))
{
invPosition = inventory.IndexOf(input.Substring(4, input.Length - 5), 0, input.Length - 5);
inventory[invPosition] = "";
Console.WriteLine(inventory[invPosition] + " has been dropped");
invSize--;
}I'm trying to create a basic code which allows you to summon an object into an array and remove it.
The command words for these are "GET" and "DROP".
At this position of the code, I want the user to type "DROP x". I want my code to check if 'x' has already been summoned. If it has, I want x to be dropped and removed from the array. To do this, I am trying to use the IndexOf Method, to try and find the position of 'x' in the array and remove it. However, my error for IndexOf says "cannot convert from string to System.Array". Can anybody help?
Try the find function.
Here is the example from the article I linked:
string str1( "Alpha Beta Gamma Delta" );
string::size_type loc = str1.find( "Omega", 0 );
if( loc != string::npos ) {
cout << "Found Omega at " << loc << endl;
} else {
cout << "Didn't find Omega" << endl;
}
You are looking for the std::basic_string<> function template:
size_type find(const basic_string& s, size_type pos = 0) const;
This returns the index or std::string::npos if the string is not found.
Just subtract the string address from what strchr returns:
char *string = "qwerty";
char *e;
int index;
e = strchr(string, 'e');
index = (int)(e - string);
Note that the result is zero based, so in above example it will be 2.
You can also use strcspn(string, "e") but this may be much slower since it's able to handle searching for multiple possible characters. Using strchr and subtracting the pointer is the best way.
scanf("%c",&c)..you are getting input a character.
Working copy would besomethign like:-
char c; // you want to find the character not some integer.
int result;
printf("Please enter string: ");
scanf("%s", str1);
printf("Please enter char: ");
scanf("%d", &c);
result = indexof(str1, c);
printf("Result value: %d\n", result);
Working example:-
#include <stdio.h>
#include <string.h>
int indexof(char *str, char c)
{
int i;
for (i = 0; i < strlen(str); i++)
{
if (str[i] == c)
return i;
}
return -1;
}
int main()
{
char c;
int result;
char str1[100];
printf("Please enter string: ");
scanf("%s", str1);
printf("Please enter char: ");
scanf(" %c", &c);
result = indexof(str1, c);
printf("Result value: %d\n", result);
}
Since c is int:
int indexof(char *str, int c)
{
int i;
for (i = 0; i < strlen(str); i++)
{
if ((str[i]-'0') == c) // convert char to int
return i;
}
return -1;
}