Question

Program in C Write a function that replaces the contents of a string with the string...

Program in C

Write a function that replaces the contents of a string with the string reversed.

Homework Answers

Answer #1

Code:

#include <stdio.h>

//Here is the actual function for the question

void str_reverse(char *str){

int len=0;

//calcualting length

while(str[len]!='\0'){

len++;

}

int front=0,rare=len-1;

//inplace string reverse

//replacing the first and last character of the string

while(front<rare){

char temp=str[front];

str[front]=str[rare];

str[rare]=temp;

//repeating the process untill front and rare crosses each other

front++;

rare--;

}

}

//Here is the test question

int main(void) {

char s[100];//creating a string

printf("Enter a string:");

scanf("%s",s);

str_reverse(s);//passing it the function

printf("Same string after passing to the function str_reverse\n");

printf("%s\n",s);

printf("Contents of the string has been changed");

return 0;

}

Snapshot:

Sample run:

Enter a string:samantha
Same string after passing to the function str_reverse
ahtnamas

Please give it thumbsup and comment below for any problem in the answer

Know the answer?
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
Similar Questions
Write a recursive function count_digits that counts all the digits in a string. Program: C
Write a recursive function count_digits that counts all the digits in a string. Program: C
Write a short RISC-V assembly program that operates on a NULL terminated string of arbitrary size...
Write a short RISC-V assembly program that operates on a NULL terminated string of arbitrary size (use your favourite phrase when you define it with DC for testing). The program (no need to define a function) scans the string and replaces every lower case character with the corresponding upper case. Your program prints the string before and after converting it to upper case.
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
Write a program in C that extracts the tokens from a string. Given a string as...
Write a program in C that extracts the tokens from a string. Given a string as input, the program should print on the screen each token on a new line. For example given the following string as input: “hello there my friends” the program should generate: I am a programmer
Write a Java method that takes a String as Input and replaces the first two occurrencesof...
Write a Java method that takes a String as Input and replaces the first two occurrencesof a given character to another provided character.   For example, if the input String is “Java is Great” and asked to replace the letter a with x then the output would be: “Jxvx is Great” The method takes a String and two characters and returns the modified String. Please make your own assumptions if needed, you do not need to write the main.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a function to check whether string s1 is a substring of string s2. The function...
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2) Write a program to test the function.
In C++ Write a program that will convert a string of binary digits to their decimal...
In C++ Write a program that will convert a string of binary digits to their decimal equivalent. For convenience limit the binary number to 16 bits. Write the decimal equivalent to the screen. For this program you must read in the binary number as a string type. If you were to enter a large binary number like 110110001100111 as a decimal value it would be too large to fit in an int type.
Write a c++ trim function that takes an input of string and output the string by...
Write a c++ trim function that takes an input of string and output the string by trimming away all non-alphabetic letters.
Write a program in c++ that uses a stack to test whether a given string is...
Write a program in c++ that uses a stack to test whether a given string is a palindrome.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT