Question

Write a C function that receives a character array (string), and it will check the characters...

Write a C function that receives a character array (string), and it will check the characters of the array to figure out whether the string has a given valid pattern. The required pattern will be given to you. In this function, for instance, you should check if the ascii code of an element of the character array is between a range. The ascii table of all the printable characters will be provided to you. See the last page of this guideline. [

0 0
Add a comment Improve this question Transcribed image text
Answer #1

So, basically we have given a string and a range of ascii values and we have to check to given string's characters are in the given range.

If they are within the range, the pattern is valid otherwise the pattern is considered as invalid.

Code :

#include <stdio.h>
#include <stdbool.h>
#include<string.h>

//this is our function declaration
bool checkPattern(char[], int, int);

int main()
{
// initializing the character array of string
char string[] = {'h','i','i','c','h','e','g','g'};
  
//setting the range of ascii values
int start = 65;
int end = 122;
  
//enter into if and only if function return true
if(checkPattern(string, start, end))
{
printf("You have entered a valid pattern");
}
else
{
printf("You have entered an invalid pattern");
}

return 0;
}

//body of our function
bool checkPattern(char string[], int start, int end)
{
int n = strlen(string);
for(int i=0; i<n; i++)
{
if( string[i] < start || string[i] > end)
{
return false;
}

}

return true;
}


----------------------------------------------------------------------------------------------------------------------------------------------------------

Screenshots of Code and output :

Here is the output:

just change the any char in the array to a number and you will get a output like this

it's because we have set the range from 65 to 122, any char between this range is considered as a valid character

Add a comment
Know the answer?
Add Answer to:
Write a C function that receives a character array (string), and it will check the characters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming 1) When setting a two-dimensional character array, how is the size (number of characters)...

    C programming 1) When setting a two-dimensional character array, how is the size (number of characters) in the second dimension set? Select an answer: The number of elements are equal to the average size of all the strings. To the length of the longest string; you don't need to add one because the first array element is zero. To the length of the longest string, plus one for the null character. The second dimension is equal to the number of...

  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • D.1 [3] Define a function called funD1...) that receives a string and returns a new string...

    D.1 [3] Define a function called funD1...) that receives a string and returns a new string with all the characters in the original string in an EVEN position. As an example, the following code fragment: print (funD1('abcde')) should produce the output: ace D.2 [6] Define a function funD2(...) which receives a list ist that contains single letters, single digits and single special characters and the list contains at least one element of each type). The function returns a string that...

  • C Question: Write a function p3 which receives a C string as a parameter, and an...

    C Question: Write a function p3 which receives a C string as a parameter, and an array of integers which will serve as indices into the string. The third parameter is the length of the integer array. The function jumbles the characters in the string according to the indices in the array of numbers. For example: char course [] = "CSC 373 Computer Systems I"; int indices[ ] = {0,1,4,5,6,7,0,1,25}; Then the function call p3(course, indices, 9); would cause course...

  • write a Node.js function searchString() that receives three parameters: a positive integer in the range of...

    write a Node.js function searchString() that receives three parameters: a positive integer in the range of 1 to 20, a target string of characters of that length, and a single character. It then checks whether or not the target string contains the given character. If it does, this function returns the index of the target string where the character is first found. Otherwise, it returns -1.

  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • C++ please! In this program you will be outputting the characters that map to the ASCIl...

    C++ please! In this program you will be outputting the characters that map to the ASCIl codes 32 through 126. You will need a loop to iterate through the input values and output the corresponding character. This mapping is shown in appendix A in your Gaddis text book. Your program will be reading in two unsigned integer values. The prompt for read will be Enter lower and upper values You need to check that both values are in the range...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Question 8 A C-string is a sequence of characters terminated by the null character. True False...

    Question 8 A C-string is a sequence of characters terminated by the null character. True False D Question 9 What is the longest C-string that can be put into this C-string variable? char s[9]; There is not enough information to determine the 8th Question 10 D AC string variable is just an array of characters without the null character. True False Question 11 5 pts If you have already removed a character in char variable ch) from the input stream,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT