Dec 10, 2014 at 12:00am UTC
if:
const int size1=10;
char arr1[size1];
How can I print out the subscript where a specific element is?
Last edited on Dec 10, 2014 at 3:33am UTC
Dec 10, 2014 at 12:11am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char str[] = "Hello World" ;
int len = strlen(str);
char ch = 'r' ;
cout<<find(str,str+len,ch) - str<<endl;
system("pause" );
return 0;
}
Last edited on Dec 10, 2014 at 12:11am UTC
Dec 10, 2014 at 12:19am UTC
more like this, but instead it prints out the subscript where each letter is
1 2 3 4
for (int x=0; x<size1;x++)
{
cout<<arr1[x] << " " ;
}
^ desired outcome
or if i want to find a specific element,it prints out its subscript
Last edited on Dec 10, 2014 at 12:29am UTC
Dec 10, 2014 at 12:30am UTC
How can I print out the subscript where a specific element is?
I thought this is what you wanted..
Else, To get your ouput,
1 2 3 4
for (int x=0; x<size1;x++)
{
cout<<arr1[x] << " " x<<endl;
}
Last edited on Dec 10, 2014 at 12:30am UTC
Dec 10, 2014 at 12:48am UTC
so that does work , but i guess thats not what i thought it would do. just how you used the find function to find r and it gave you its subscript can you apply that to an array?