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?
#include <iostream>
usingnamespace std;
// Function prototype
int search(constchar A[], char n, int value);
int main()
{
// Declarations
char x;
// Array
constchar 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(constchar A[], char n, int value)
{
int index = 0;
while (index < n && A[index] != value)
{
++index;
}
if (index < n && A[index] == value)
return index;
}
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(constchar 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;
}
/*
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>
usingnamespace std;
// Function prototype
int search(constchar A[], char value);
int main()
{
char x;
constchar 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(constchar A[], char value)
{
int letterCount = 0;
int x = 0;
while(A[x++] != '\0')
{
if(A[x] == value) ++letterCount;
}
return letterCount;
}
/*
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>
usingnamespace std;
// Function prototype
int searchArray(constchar theArray[], int arraySize, char letter);
int main()
{
constint arraySize = 15;
constchar 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(constchar theArray[], int arraySize, char letter)
{
int letterCount = 0;
for(int x = 0; x < arraySize; ++x)
{
if(theArray[x] == letter) ++letterCount;
}
return letterCount;
}
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(constchar 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.
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.
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(constchar A[], char value)
{
int CharCount = 0;
int x = 0;
while (A[x] != '\0')
{
if (A[x] == value) ++CharCount;
x++;
}
return CharCount;
}