Question

In this question, you will be implementing the following functions. int findChar(char * str, char c);...

In this question, you will be implementing the following functions. int findChar(char * str, char c); Searches for the character c in the string str and returns the index of the character in the string. If the character does not exist, returns -1

int replaceChar(char * str, char c1, char c2); Searches for the character c1 in the string str and if found, replace it with c2. The function returns the number of replacements it has performed. If the character does not exist, returns 0.

int removeChar(char * str1, char * str2, char c); Creates a copy of str1 into str2 except for the character c that should be replaced with ‘*’

For example, if str1=”Hello World” and c=’l’ then the function should make str2=”He**o Wor*d”

int isPalindrome(char * str)

Checks to see if a string is Palindrome(reversible). If it is, returns 1, otherwise returns 0. A palindrome string reads similarly from left to right and from right to left like madam, level, radar, etc.

int reverseString(char * str) Gets a string, reverses it and returns the number of characters in that string.

Develop a C code that implements the functions described above and use main function to call and test your code.

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

//C program

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

int findChar(char * str, char c){
   int i=0;
   for(i=0;i<strlen(str);i++){
       if(str[i] == c)return i;
   }
   return -1;
}

int replaceChar(char * str, char c1, char c2){
   int count = 0 , i;
   for(i=0;i<strlen(str);i++){
       count++;
       if(str[i] == c1)str[i] = c2;
   }
   return count;
}

int removeChar(char * str1, char * str2, char c){
   int i;
   for(i=0;i<strlen(str1);i++){
       if(str1[i] == c) str2[i] = '*';
       else str2[i] = str1[i];
   }
}
int isPalindrome(char * str){
   int i=0 , j = strlen(str)-1;
  
   while(i<j){
       if(str[i]!=str[j])return 0;
       i++;
       j--;
   }
   return 1;
}
int reverseString(char * str){
   int i=0 , j = strlen(str)-1;
   char temp;
   while(i<j){
       temp = str[i];
       str[i] = str[j];
       str[j] = temp;
       i++;
       j--;
   }
   return strlen(str);
}


int main(){
   char str1[20] , str2[20];
   char ch;
  
   printf("Enter string : ");
   gets(str1);
  
   printf("Enter character to search : ");
   scanf("%c",&ch);
  
   int index = findChar(str1 , ch);
  
   if(index == -1){
       printf("%c not found in string %s\n",ch , str1);
   }
   else{
       printf("%c is found at index %d in string %s\n",ch ,index, str1);
   }
  
   replaceChar(str1 , 'l' , 'p');
   puts(str1);
   printf("\n\n");
   removeChar(str1 , str2 , 'p');
   puts(str2);
  
   if(isPalindrome("radar")){
       printf("\nPalindrome\n");
   }
   else printf("\nNot a Palindrome\n");
  
   reverseString(str1);
   puts(str1);
   return 0;
}

//sample output

Add a comment
Know the answer?
Add Answer to:
In this question, you will be implementing the following functions. int findChar(char * str, char c);...
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
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