Question

In: Computer Science

In this C program, the function reverseStr reverses the nth word in a string. If n...

In this C program, the function reverseStr reverses the nth word in a string. If n is larger than the number of words in the string then the function returns 0, else return the function modifies the string and returns 1.

Task: write the reverseStr function that follow the requirements below:

1. consistent with the prototype and code below

2. original string must be changed

3. DON'T use functions in string.h

Tips:

1. string input to this function is non-empty, that includes one or more non-whitespace characters

2. every consecutive word in the string is separated by single space characters and has no punctuation marks

3. string always start with a non-whitespace character

4. string is always terminated with a null '\0' character

5. the value of n > 0

Please use this program:

#include <stdio.h>

int reverseStr(char *string, int n);

int main(void)

{

char input[] = "Pizza is much better than burger";

int n = 4;

printf("Original: %s\n", input);

printf("n = %d\n", n);

if (reverseStr(input, n))

{

  printf("New: %s\n", input);

}

else

{

  printf("Error!!!! Number of words in %s is less than %d\n", input, n);

}

return 0;

}

Expected Output:

Original: Pizza is much better than burger

n = 4

New: Pizza is much retteb than burger

Note: Please screenshot your output window

Solutions

Expert Solution

All the explanation is in the comments of the code itself.

Code--

#include <stdio.h>
int reverseStr(char *string, int n);
int main(void)
{
char input[] = "Pizza is much better than burger";
int n = 6;
printf("Original: %s\n", input);
printf("n = %d\n", n);
if (reverseStr(input, n))
{
printf("New: %s\n", input);
}
else
{
printf("Error!!!! Number of words in %s is less than %d\n", input, n);
}
return 0;
}
int reverseStr(char *string, int n)
{
int word=1,i=0;
//reach the index on the nth word
while(word<n)
{
   //if nth word does not exist return 0
   if(string[i]=='\0')
   {
       return 0;
       }
       //if it is a space increase the word by 1
if(string[i]==' ')
{
word++;
}
i++;
}
  
int end=i+1;
//reach the last letter of the word to be reversed
while(string[end]!=' ' )
{
if(string[end]=='\0')
   break;
end++;
}
end--;
//reverse the word
   while(i<end)
{
char temp=string[i];
string[i]=string[end];
string[end]=temp;
i++;
end--;
}
return 1;
}

Output Screenshot--

Note--

Please upvote if you like the effort.


Related Solutions

Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Use C to Write a program that takes the array reverse_me and reverses the order of...
Use C to Write a program that takes the array reverse_me and reverses the order of the elements (i.e., the first element and the last element are swapped, the second element and the second-to-last element are swapped, etc.). Store the reversed array in reversed_arr. Finally, print out the reversed array as a string.
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
C++ function to a string into enum function to a enum into a string check valid...
C++ function to a string into enum function to a enum into a string check valid / loop ( asking a couple of times until answer invaild) For example Enum Fruit ( APPLE, STRAWBERRY, BLUEBERRY, BLACKBERRY) output should be what is your favorit fruit? : strawberry you will have STRAWBERRY. what is your favorite fruite : peach invaild TIA
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 C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT