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 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...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
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]
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
in basic c++ program please!! Write a word search and word count program. Assign the following...
in basic c++ program please!! Write a word search and word count program. Assign the following text to a string constant. For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him.Whoever believes in him is not condemned, but whoever does not believe stands...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT