QUESTION

if:
const int size1=10;
char arr1[size1];

How can I print out the subscript where a specific element is?
Last edited on
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
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] << " ";
 }


a b c d e f g h i j 
0 1 2 3 4 5 6 7 8 9 
^ desired outcome

or if i want to find a specific element,it prints out its subscript
Last edited on
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
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?
can you apply that to an array?

In the first example, is str not an array??
Topic archived. No new replies allowed.