Question

using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

using c language

String Challenge

Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many characters should appear in the sequence where N will be at least 1. Your goal is to determine if the second string exactly matches the pattern of the first string in the input.

For example: if str is "++*{5} jtggggg" then the second string in this case does match the pattern, so your program should return the string true. If the second string does not match the pattern your program should return the string false.

Examples

Input: "+++++* abcdehhhhhh"
Output: false

Input: "$**+*{2} 9mmmrrrkbb"
Output: true

---------------given the following code what can you edit on it to get the result

#include <stdio.h>

#include <string.h>

void StringChallenge(char str[]) {

  

  // code goes here  

  printf("%s", str);

}

int main(void) {

   

  // keep this function call here

  StringChallenge(gets(stdin));

  return 0;

    

}

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

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


bool StringChallenge(char str[]) {
   char delim[] = " ";
   int init_size = strlen(str);
   char *ptr = strtok(str, delim);
   char a[init_size],b[init_size];
  
   /* breaking the string in two parts */
   strcpy(a,ptr);
   ptr = strtok(NULL, delim);
   strcpy(b,ptr);
  
   int i=0,j=0;
  
   /* running the loop while first string is not empty */
   while(a[i]!='\0')
   {
       /* evaluating on '+' symbol */
       if(a[i]=='+')
       {
           if(isalpha(b[j]))
           {
               i++;
               j++;
               continue;
           }
           else
           {
               return false;
           }
       }
      
       /* evaluating on '$' symbol */
       else if(a[i]=='$')
       {
           if(isdigit(b[j]))
           {
               i++;
               j++;
               continue;
           }
           else
           {
               return false;
           }
       }
      
       /* evaluating on '$' symbol */
       else if(a[i]=='*')
       {
           if(a[i+1]=='{')
           {
               i+=2;
               int N = (int)a[i]-48;
               int k;
               char same;
               if(isalpha(b[j]))
                   same = b[j];
               else
                   return false;
               for(k=1;k<=N;k++)
               {
                   if(b[j]==same)
                   {
                       j++;
                   }
                   else
                   {
                       return false;
                   }
               }
               i+=2;
           }
           else
           {
               int k ;
               char same ;
               if(isalpha(b[j]))
                   same = b[j];
               else
                   return false;
               for(k=1;k<=3;k++)
               {
                   if(b[j]==same)
                   {
                       j++;
                   }
                   else
                   {
                       return false;
                   }
               }
               i++;
           }
       }
   }
   if(b[j]!='\0')
   {
       return false;
   }
   return true;
}


int main(void) {

// keep this function call here
   char str[200];
   bool a = StringChallenge(gets(str));
   if(a)
   {
       printf("true\n");
   }
   else
       printf("false\n");
   return 0;

}

//Output

C:\Users\AdiShakti NavDurga\Desktop\stringchallenge ++*{5} jtggggg true +++++* abcdehhhhhh false $**+*{2} 9mmmrrrkbb true

Add a comment
Know the answer?
Add Answer to:
using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...
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
  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • 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);...

  • PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome...

    PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome is a string of characters that is read the same forwards as it is backwards. For example, racecar' is a palindrome; reversing the order of the characters leaves the string unchanged. Write a otherwise. function called isPalindrome that accepts one input and returns one output. The input str is a string of characters, and the output is 1 if str is a palindrome, For...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • use c code to Develop a function void printToggled(char str[]) that gets a string as a...

    use c code to Develop a function void printToggled(char str[]) that gets a string as a parameter and prints every lowercase character as an uppercase one and vise versa. For example, if the string submitted as a parameter is Hello WorlD! The function should print hELLO wORLd! Hint:answer must include output

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...

    Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining *   if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: *    result = is_palindrome(str, first_index, last_index) * * parameters - *    str - the string to test *    first_index -...

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