Question

C Programming Language only please. Your help is greatly appreciated. Will give thumbs up for quality work. Lab 8 (this is to be turned in) Palindrome is a string that is the same as backwards or forwards “otto” “ababa” “noon” “ababbbbaba” In this lab we will work with strings (so we will be using character arrays). 1. Write a program that reads two strings strl and str2 from the keyboard 2. Print both strings 3. Write a function computeLength and call this function in main to compute the length of the string strl and str2. 4. if the string strl is of length 1 the program will terminate 5. if the string str2 is of length 0 the program will terminate Next A prefix of a string is a nonempty string that is an initial segment Example “moondog” has prefixes of “moo”, “m”, “moon”, “mo”, “moond”,. 6. Write a function isPrefix (str1, str2) that returns 1 if the string str2 is a prefix of strl substring of strl then isPrefix should return a ONE , if not return a ZERO. See class discussion for the definition 7. Call this function with strl and str2 in main and output your result 8. Now write a function isPalindrome(str) that returns a l if str is a palindrome and 0 otherwise. To get full credit use isPrefix in your function 9. Call your function is Palindrome (str1) 10. Output the correct conclusion either PALINDROME or NOT PALINDROME You are NOT to use any standard library functions except for I/O functions.

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

#include<stdio.h>
//method to find lenght of str
int computeLength(char *str)
{
   int i=0;
   while(str[i]!='\0')//runs upto end of the string
   i++;
   return i;//returning length
}

//method to find prefix
int isPrefix(char *str1,char *str2)
{
   int i=0;
   while(str2[i]!='\0')
   {
       if(str1[i]==str2[i])
       i++;  
       else
       break;//if not prefix
   }
   int n= computeLength(str2);
   if(i==n)//means prefix
   return 1;
   return 0;//other wise returning 0
}

int isPalindrome(char *str)
{
   int n =computeLength(str);
   int i=0,j=n-1;
   while(i<n)
   {
       if(str[i]==str[j])
       {
           i++;
           j--;
              
       }
       else break;  
   }
   if(i==n)return 1;//if palindrome
   return 0;
}

int main()
{
   char str1[100],str2[100];
  
  
   printf("Enter string 1:");
   scanf("%s",str1);
  
   if(computeLength(str1)<=1)
   return 0;//terminations program if length =1

   printf("Enter string 2:");
   scanf("%s",str2);
  
   if(computeLength(str2)==0)
   return 0;//terminations program if length =1  
  
  
   if(isPrefix(str1,str2)==1)printf("Prefix\n");
   else printf("Not a prefix\n");
  
   if(isPalindrome(str1)==1)printf("Palindrome\n");
   else printf("Not a palindrome\n");
  
   return 0;
}

output:

Enter string 1:amma
Enter string 2:am
Prefix
Palindrome


Process exited normally.
Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
C Programming Language only please. Your help is greatly appreciated. Will give thumbs up for quality...
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
  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

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

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a...

    From the Tony Gaddis text, the chapter on C-String and Class String: String Length: Write a Function that passes in a C-String and using a pointer determine the number of chars in the string.                                           Data:   “This is a test string for string length” Prt String Backwards: Write a Function that passes in a C-String and prints the string backwards.      Data: “This is a test string for string backwards” replaceSubstring: Write a Function that accepts three C-Strings – str1,...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

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