jmarie
jmarie

Reputation: 11

Finding consecutive characters in an array

I have a boolean function that evaluates a 1d array of characters. It has two parameters: a 1d array of characters , and a char c. I want the function to return true if the given char c appears at least four consecutive times within the given array, otherwise it will return false.

I don't know how to start or complete this function at all. Please help! Thanks.

Upvotes: 0

Views: 2105

Answers (2)

Sangkaran
Sangkaran

Reputation: 38

Simple problem. If this is your homework then you shouldn't be doing this. Your question needs to be changed. Firstly give it a try before asking and then once you are done trying you can post the errors or the snippets of codes that you are unsure of and then ask for help. Else you are not going to learn anything. Got a simple solution to your problems. I'm not going to give you the complete solution but instead a guide to help you with your question.

In my opinion string is always a better choice to use instead of char because of the functions that come with that package. Char is just plain old annoying (again in my opinion) unless your question or whatever you are doing this program for requires you to use char.

First, Create your main program -> create your array and initialize it if you want or you can prompt the user for their input. whichever works. use the "bool" data type to create your Boolean variable. Prompt the user to input the char value to check for.

Now call the function and provide the parameters. I'm guessing the function is where you are stuck with so i'm going to provide you the snippets from the code that i wrote for this question.

bool check(char* <array_name>, char* <array_name>) //for the array list and the 
                                                   //value to check for

{
     int size; 
     size = strlen(<array_name>); //to get the size of the array (array list)

     int counter=0; //to keep count of the occurrence of the char to check

     for(int x=0; x<size; x++) //ar = array list and token = char to check
     {
         if(ar[x]==token[0]) //check for each iteration if token is in ar[x]
             counter++;      //if it is then counter increases by 1
         else 
             counter = 0; //To reset the value to 0 if its not consecutive.

         if(counter == 4) //to stop the loop when 4 consecutive values has been found.
             break;
     }

     if(counter >= 4) //as per your requirement 4 or above
                return true;
     else
                return false;   
}

EDIT: This is to check the values just until 4 consecutive values of what you are searching for is found and to end the loop. If you want it in a different way then please feel free to comment on this answer. You can always add another counter or anything at all to check how many consecutive times the value is found. For example 1,1,1,1,2,3,4,1,1,1,1,2,3,4,1,1,1,1,2,3,4.

The counter for that will be 3 since it happens 3 times with each time repeating the same value for 4 times consecutively.

If this is your homework then you better study properly because it's a really simple problem and your shouldn't be asking for a solution but instead ask for guidance and try first.

Good luck! If you need further clarification or help just comment on this.

Upvotes: 0

Dillon Dunn
Dillon Dunn

Reputation: 138

I hope I'm not doing you're homework for you ;). So here's the sudo-code for this problem to help you get started

The first thing you would want is the method header that returns a boolean, and has a parameter for an array of characters and a char

The next step would be to create a counter and run a loop to sift threw every character in the array. Every time you encounter that specific character in the array you would add one to the counter, if the next character isn't the one you want then you would reset the counter to 0. Then add a conditional in the loop to check if the counter reaches 4, if so you would return true. If it never reaches 4 then you would want to return false. Go ahead and try to code that up and see if you get it.

Upvotes: 1

Related Questions