-1

So basically I have an array which looks like this:

char array[25];

I have an 'X' put in there, and 24 'O's. I am trying to create a function where I can find the location of 'X' in the array, so then I can move it whenever a control is typed in. Here is some code to get an idea on what I am thinking, I just can't put it together.

int findX(){
    //make this find the location of X out of the 25 char array.
    //then return the slot of X as a number, like if it's in the 20th slot, then  it will return 20 ?
    //return(locationOfX);
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Zackery Page
  • 23
  • 1
  • 3
  • 6
    Perhaps you need [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? Because it should teach you all you need to know to solve your problem yourself. – Some programmer dude Oct 16 '17 at 06:49
  • Use std::string! –  Oct 16 '17 at 07:01

4 Answers4

1
for(int i = 0; i <= array.Length; i++)
{
    if(array[i] == 'X')
    {
        return i;
    }
}

This should do it, I had no chance testing it, but it should work

Adalcar
  • 1,458
  • 11
  • 26
MrHipppo
  • 11
  • 1
0

Pass in the array and the number of elements.
1. The manual approach:

int findX(char arr[], int count){
    for (int i = 0; i < count; i++){
        if (arr[i] == 'X'){
            return i;
        }
    }
    return -1;
}

2. Use std::find function:

int findX2(char arr[], int count){
    return std::find(arr, arr + count, 'X') - arr;
}

Slight twist with the std::distance function:

int findX2(char arr[], int count){
    return std::distance(arr, std::find(arr, arr + count, 'X'));
}

3. Pass in the beginning and the end of the array:

int findX3(char* arrbegin, char* arrend){
    return std::distance(arrbegin, std::find(arrbegin, arrend, 'X'));
}

and use like:

std::cout << findX3(std::begin(arr), std::end(arr));

4. Template function left as an exercise.
That being said, prefer std::vector or std::array to raw arrays.

Ron
  • 14,674
  • 4
  • 34
  • 47
0

I think Ron's answer is more correct because actually I think in C there isn't a function like "XXX.length()", so you need to pass the length of the array to the function.

TX.Wang
  • 1
  • 1
  • 2
    While I appreciate the feedback I would like to point out that comments should be posted as comments, not as answers. Please build up some reps to be able to post comments. – Ron Oct 16 '17 at 07:34
0

In language C you can use strstr function.

#include "string.h"
int findX(char array[])
{
   char *result = strstr(array, "s");
   return result - array;
}
int main()
{
   char array[25];
   int index = findX(array);
}

in C++ use std::find() or just change array to std::string and use string::find

here is the code:

#include <string>

int main()
{
    std::string array = "oooooooooosoooooooooooooo";
    int index = array.find("s");
    return 0;
}
Gor Asatryan
  • 904
  • 7
  • 24
  • strstr requires that the argument is a string. OP's array is not null terminated and therefore is not a string. – eerorika Oct 16 '17 at 07:35