*USING C LANGUAGE with no errors detected String Manipulation: Reverse. Write a
ID: 3858098 • Letter: #
Question
*USING C LANGUAGE with no errors detected
String Manipulation: Reverse. Write a function called Reverse that accepts as a parameter a char array (or char pointer) and returns a void type. The function reverses the contents of the array (or string) being passed. Example: the string "Hello" will be reversed to "olleH". You have to be careful about the '' symbol to keep it at the end of the array and watch out for odd and even length of strings. Test your function and display the string (array) before and after it is reversed. IMPORTANT: The function does NOT print the string in reverse: it actually reverses it in the memory. String Tokenization Write a function called ParseSentence that takes as input parameter a null (i.e. '1') terminated string s, where s would contain an English sentence. Assume that the delimiters are space, comma, semicolon and period. The function extracts each word from the sentence (without any punctuation or spacing) and then prints one word per line. The function returns a void type. For example: char str "hello world, how are you today.": ParseSentence (str): would print the following: hello world how are you todayExplanation / Answer
Answer for B
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Reverse(char * array) {
int start = 0;
int end = 0;
char temp;
end = strlen(array) - 1;
printf("%s ", array);
while (start < end) {
temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
int main() {
char arrayLength[] = "ReverseProgram";
Reverse(arrayLength);
puts(arrayLength);
return(0);
}
Output For Answer B
ReverseProgram margorPesreveR
Answer for C
#include<string.h>
#include<stdio.h>
void ParseSentence(char str[])
{
char *pointerChar;
pointerChar = strtok (str," ,.-;");
while (pointerChar != NULL)
{
printf ("%s ",pointerChar);
pointerChar = strtok (NULL, " ,.-;");
}
}
int main()
{
char str[] = "-Welcome - to ,this ;new world"; //you can pass even "" which means null value
ParseSentence(str);
return 0;
}
Output for Answer C
Welcome
to
this
new
world
Combining QuestionB and C in one program
#include<string.h>
#include<stdio.h>
void Reverse(char * array) {
int start = 0;
int end = 0;
char temp;
end = strlen(array) - 1;
printf("%s ", array);
while (start < end) {
temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
void ParseSentence(char str[])
{
char *pointerChar;
pointerChar = strtok (str," ,.-;");
while (pointerChar != NULL)
{
printf ("%s ",pointerChar);
pointerChar = strtok (NULL, " ,.-;");
}
}
int main()
{
char str[] = "-Welcome - to ,this ;new world";
ParseSentence(str);
char arrayLength[] = "ReverseProgram";
Reverse(arrayLength);
puts(arrayLength);
return 0;
}
Ouput for the AnswerBand C together
Welcome
to
this
new
world
ReverseProgram margorPesreveR
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.