-7

I am trying to create a program that runs certain commands ( Pali() is the command ) for example, when the user types in Pali(bob) the program checks if the word inside the () is a palindrome. Also, the command Pali() must be exactly typed out like that or an error message will come up.

I plan on using strtok to parse the string but not quite sure how I can do that to check just the characters "Palin()" and ignore the content inside the parentheses. Also how would I be able to pull the content out of the parentheses so I can test if its a palindrome?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Sherin
  • 13
  • 5
  • Why can't you just check if the first character is P, the second is a, the third is l, the fourth is i, the fifth is ( and the last is ) ? – user253751 Sep 14 '18 at 03:10
  • @immibis yes I suppose I could do that but then how would I extract the content that is in the parenthesis? – Sherin Sep 14 '18 at 03:36
  • Since you have a very specific format, obviously the content between the parentheses is the 6th character up to the 2nd-to-last... – user253751 Sep 14 '18 at 03:36
  • 7
    Please do not deface your question. Once posted it is property of StackOverflow, and it should be left up, so that it can allow others with similar problems to use it to help them. – Hovercraft Full Of Eels Sep 14 '18 at 13:04

1 Answers1

0

you'll be needing strncmp function with string.h header to compare (check) if the characters Palin( have been entered or use KMP/Needle In The Stack algo to match the strings.

For taking out the content in the () from Palin() in an array say destination[]:

 #include<stdio.h>
 #include<string.h>
 #define MAX 100

 int main()
 {
    char source[]="palin(hello)";
    int len=strlen(src);
    char destination[MAX];
    memset(destination,0,MAX);

    //READ ONLINE ABOUT STRNCPY

    strncpy(destination,source+6,len-7);
    printf("%s\n",destination);

    return 0;
 }

OUTPUT: Hello

Gaurav
  • 1,570
  • 5
  • 17