Search Array

Hello, I am still pretty new to C++ I don't have much class left but I am having a problem getting my program to work.

The first program needs to take your name and have you search for a character and return how many times it was found in the array.

the second needs to have the search function reading in three inputs, array size, array and character to be searched for.

This is what I have so far, it returns weather the character was found or not but it will not tell me how many times. and for some reason if you put n it will tell you it was found at position for and if I use N it says it was found at position 5?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;
// Function prototype
int search(const char A[], char n, int value);

int main()
{
	// Declarations
	char x;
	// Array
	const char nameArray[14] = {"Your Name"};
	// Enter the letter to search for
	cout << "Enter the letter you would like to search for." << endl;
	cin >> x;
	// cout if it was found or not
	if (search(nameArray, 14, x) == -1)
		cout << "This letter was not found" << endl;
	else
		cout << "Success! Found " << x << " in position " << search(nameArray, 5, x) << "." << endl;
}




// Function Definition
int search(const char A[], char n, int value)
{
	int index = 0;
	while (index < n && A[index] != value)
	{
		++index;
	}
	if (index < n && A[index] == value)
		return index;
}



Thank you in advance!
Last edited on
You have a couple of problems with your search function.

You're trying to use index as both the index into the string to search and as the count of the number of times the characters was found. These are two different things and need two different variables.

Line 20: why are you using 5 for the length? You used 14 in the previous search.

Line 31: Your while loop exits at the first match.

37
38
39
40
41
42
43
44
45
46
47
int search(const char A[], char n, int value)
{   int index = 0;
    int cnt = 0;

    while (index < n)
    {   if (A[index] == value)
            cnt++;
        ++index;
    }
    return cnt;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Your code didn't match what was being asked. Here, fixed it for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
int search(char nameArray[], int nameArraySize, char x)
{
    int times = 0;
    for (int i=0;i<nameArraySize;i++)
        if (nameArray[i] == x)
            times++;
    return times;
}
int main()
{
    char nameArray[] = "Your Name", x;
    std::cout << "Enter the letter you would like to search for." << std::endl;
    std::cin >> x;
    if (search(nameArray, sizeof(nameArray), x))
        std::cout << "Success! Found '" << x << "' " << search(nameArray, sizeof(nameArray), x) << " times." << std::endl;
    else
        std::cout << x << " was not found." << std::endl;
}
Last edited on
Take a look At this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Write a main function that creates an array with your name,
prompts the user to enter a letter and searches if this letter is in your name.
It prints how many times the letter was found in your name. (6 points)
*/

#include <iostream>

using namespace std;

// Function prototype
int search(const char A[], char value);

int main()
{
    char x;
    const char nameArray[] = {"nivke Ryadiond"};
    // Enter the letter to search for
    cout << "Enter the letter you would like to search for." << endl;
    cin >> x;
    // cout if it was found or not
    int letterCount = search(nameArray, x);
    if (letterCount < 1)
        cout << "This letter was not found" << endl;
    else
        cout << "Found !, " << "it appears " << letterCount << " times in Your Name";
}




// Function Definition
int search(const char A[], char value)
{
        int letterCount = 0;
        int x = 0;
        while(A[x++] != '\0')
        {
            if(A[x] == value) ++letterCount;
        }
        return letterCount;
}



#Edited: i have removed the Op's name
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Write a char search function that takes 3 parameters as input:
 the array, the size of the array and the char value to find in the array.
  The function returns the number of times the char occurs in the array. (7 points)
*/

#include <iostream>

using namespace std;

// Function prototype
int searchArray(const char theArray[], int arraySize, char letter);

int main()
{
    const int arraySize = 15;
    const char nameArray[arraySize] = {"nivke Ryadiond"};
    // Enter the letter to search for
    char x;
    cout << "Enter the letter you would like to search for." << endl;
    cin >> x;
    int letterCount = searchArray(nameArray, arraySize, x);
    cout << x << " Was found " << letterCount << " times";
}




// Function Definition
int searchArray(const char theArray[], int arraySize, char letter)
{
        int letterCount = 0;
        for(int x = 0; x < arraySize; ++x)
        {
            if(theArray[x] == letter) ++letterCount;
        }

        return letterCount;
}


#Edited removed the Op's name from the post
Last edited on
Thank you all for your input, huge help! worked out great. however I'm still just a tad unclear about what the statement on line 37 of the first one does. is that stating that while the array has a value that's not \0 it will execute the if statement? and is the x++ incrementing each time that it checks a character?
The first search algorithm posted by bazetwo is defective.

33
34
35
36
37
38
39
40
41
42
int search(const char A[], char value)
{
        int letterCount = 0;
        int x = 0;
        while(A[x++] != '\0')
        {
            if(A[x] == value) ++letterCount;
        }
        return letterCount;
}


You're correct, that the intent was to iterate through the string until a null terminator is found.

The problem is that the increment of x is in the wrong place.
Let's take the case where x is 0.
line 37 tests if A[0] is non-zero, then post increments x so x is now 1.
line 39 now tests A[1]. A[0] is never tested.
bazetwo's second post corrected this by using a for loop instead.

Last edited on
The while loop is running while the character in question is not '\0', a NULL character. All strings end with a NULL character, so that's an alternate way of looping through the whole array without using the for loop and knowing the array's size.
Thanks, very helpful, Also for the people who posted code, sorry I added my name in there, but could you edit it and omit my name please, Thank you
line 37 tests if A[0] is non-zero, then post increments x so x is now 1.
line 39 now tests A[1]. A[0] is never tested.


Line 37 checks if we are in the end of a char array. x original value is used before the increment is made. so A[0] will always be tested .
A[0] is tested correctly on line 37. Not on line 39.

When line 39 is reached, x has already been post incremented and is therefore 1 at the time line 39 is executed. A[0] is never compared to value.
I did run into the problem where it wouldn't find k so I changed this and it seems to work ok now.

1
2
3
4
5
6
7
8
9
10
11
int search(const char A[], char value)
{
	int CharCount = 0;
	int x = 0;
	while (A[x] != '\0')
	{
		if (A[x] == value) ++CharCount;
		x++;
	}
	return CharCount;
}
Topic archived. No new replies allowed.