Question

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.

  1. Write a print_string function that prints the characters in a string to screen on- by-one.
  2. 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 they are identical.
  3. Write a is_palindrome function. The function is required to be case insensitive and should only consider the letters and the numbers in the input string, i.e., remove all fillers (see lecture slides). Using this definition, the string “Madam, I’m Adam!” is considered a palindrome. The function is required to return 1 if the string is a palindrome and otherwise 0. (See the lecture slides for helpful hints.)

void print_string(char str[]); int is_identical(char str1[],char str2[]); int is_palindrome(char str[]);

void main(){   char str1[] = "Hello World";   char str2[] = "Madam, I'm Adam!";   char str3[] = "hello world";

print_string(str1);   printf("\n");   print_string(str2);   printf("\n");

(is_identical(str1,str2)) ? printf("Identical!\n") : printf("No identical!\n");

(is_identical(str1,str3)) ? printf("Identical!\n") : printf("No identical!\n");

(is_palindrome(str1)) ? printf("Palindrome!\n") : printf("No palindrome!\n");   (is_palindrome(str2)) ? printf("Palindrome!\n") : printf("No palindrome!\n"); }

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

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

1)function to print the string one-by-one:-

void print_string(char str[]){

int i,n=strlen(str); //strlen() is predefined in string.h header file

for(i=0;i<n;i++)

printf("%c",str[i]);

printf("\n");

}

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

2)Function to check whether two functions are identical or not

int is_identical(char str1[],char str2[]){

int i,n,m,c=0;

n=strlen(str1),m=strlen(str2);

if(n!=m) return 0;

else{

for(i=0;i<n;i++) //tolower,isalpha&isdigit functions are predefined in ctype.h header file

if(tolower(str1[i])!=tolower(str2[i])) //converting into lowercase since "case insensitive"

{ c=1; //that means character can be in any case(upper/lower)

break;}

if(c==0) return 1;

else return 0;}

}

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

3)function to check whether the string is palindrome or not

int is_palindrome(char str[]){

int i,n=strlen(str),x=0,j=0,y=0;

for(i=0;i<n;i++) //counting number of alphabets and the digits in the string

if(isalpha(str[i])||isdigit(str[i]))

x++;

char tmp[x];

for(i=0;i<n;i++)

if(isalpha(str[i])||isdigit(str[i]))

tmp[j++]=tolower(str[i]);

for(i=0;tmp[i]!='\0';i++)

if(tmp[i]!=tmp[x-i-1])

{y=1;

break;

}

if(y==0)

return 1;

else return 0;

}

Add a comment
Know the answer?
Add Answer to:
In C programming Write the implementation for the three functions described below. The functions are called...
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 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...

  • C Programming Language only please. Your help is greatly appreciated. Will give thumbs up for quality...

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

  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

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

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

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