How would you search for second occurence of the same character on the line? For example:
ftp,tcp,SF,attack,smurf
How would you search for 3rd comma (before attack) and save the word 'attack' in the buffer?
~Dmitry
Printable View
How would you search for second occurence of the same character on the line? For example:
ftp,tcp,SF,attack,smurf
How would you search for 3rd comma (before attack) and save the word 'attack' in the buffer?
~Dmitry
that should work as far as i knowCode:char searchThis[50]="ftp,tcp,SF,attack,smurf";//what to search through
char * pointer=new char[500];//pointer with oversized buffer
pointer=strstr(searchThis,"*,*,*,");//I think the wildcards should work, otherwise just do strstr three times for ","
pointer[0]='\b';//deletes the comma returned in the pointer
strtok(pointer,",");//deletes the comma after the word attack along with everything after it
cout<<pointer<<endl;
With character arrays:
Or it's easier with the string type. I don't use it (string type), but I think there is a find() function or something.Code:char str[80] = "ftp,tcp,SF,attack,smurf";
char *p;
char word[20] = "";
p = strtok(str,",");
p = strtok(NULL,",");
p = strtok(NULL,",");
p = strtok(NULL,",");
cout << p << endl;
strcpy(word,p);
cout << "word:" << word << endl;
all I get is segmentation fault. Any idea what this means?
Code:int comma;
cout << "which comma do you want to find?" << endl;
cin >> comma;
int i;
int commaCounter = 0;
char input[] = "ftp,tcp,SF,attack,smurf";
for(i = 0; i < strlen(input); i++)
{
if(input[i] == ',')
{
counter++;
if(counter == comma)
{
cout << "we're there" << endl;
cout << "comma number " << comma << " was found at character position " << i + 1 << " in the input ." << endl;
i = strlen(input);
}
}
}
if(counter != comma)
cout << "not that many commas in the input. sorry....." << endl;