Reverse A String In C Exercise 5

  • Not using strlen.
  • Using pointer arithmetic.
  • Not using a temporary variable.

Explanation:

The accurate choice is option a. Verifying for null pointers in the program before attempting to reverse the strings is essential in order to prevent dereferencing a null pointer, which could potentially lead to a program crash.

  1. What is the expected output of the code snippet provided below?
  2. Example
    
    #include<stdio.h>
    #include<string.h>
    void reverseString(char *str)
    {
      int n=strlen(str);
    for(i=0;i<n/2;i++)
    {
          char temp = str[i];
            str[i] = str[n - i - 1];
            str[n - i - 1] = temp;
        }
    }
    int main() {
        char str[] = "palindrome";
        reverseString(str);
        printf("%s\n", str);
        return 0;
    }
    
  • emordnilap
  • palindrome
  • elordnimap
  • error

Explanation:

The accurate choice is option a. When the reverseString function in C swaps characters at the start and end of a string, it generates a reversed string.

  1. What will be the result of the code below?
  2. Example
    
    #include<stdio.h>
    #include<string.h>
    void reverseString(char *str,int l)
    {
    for(i=0;i<l/2;i++)
    {
            char temp = str[i];
    str[i]=str[l-i-1];
    str[l-i-1]=temp;
        }
    }
    int main() {
        char str[] = "hello";
        int l = strlen(str);
        reverseString(str, l);
        printf("%s\n", str);
        return 0;
    }
    
  • hello
  • hlelo
  • error
  • olleh

Explanation:

  1. Which of the following statements are true about reversing a string at a place?
  • The string should be null-terminated.
  • The string can be of length zero or more.
  • The function must allocate new memory for the reversed string.
  • The string cannot contain any spaces.

Explanation:

The accurate choice is a. To reverse a string effectively using functions such as strlen, it is necessary for the string to be null-terminated.

  1. What output will the code below produce?
  2. Example
    
    #include<stdio.h>
    #include<string.h>
    void reverseString(char *str)
    int n=strlen(str);
    for(i=0;i<n/2;i++)
    {
            char temp = str[i];
            str[i] = str[n - i - 1];
            str[n - i - 1] = temp;
        }
    }
    int main() {
        char str[] = "level";
        reverseString(str);
        printf("%s\n", str);
        return 0;
    }
    
  • elvel
  • level
  • level
  • lleve

Explanation:

The accurate choice is c. Given that the level is a palindrome, its reverse will also be "level."

  1. Which function below accurately reverses a string in C by utilizing pointers?
  2. Example
    
    void reverseString(char *str)
    {
       	 char *start = str;
        	char *end = str + strlen(str) - 1;
       	 while(start < end){
           	 char temp = *start;
           	 *start++ = *end;
          	  *end-- = temp;
        	}
    }
    
    Example
    
    void reverseString(char *str)
    {
       	 char *start = str;
       	 char *end = str + strlen(str) - 1;
        	while(start < end){
           	 char temp = *start;
            	*start = *end;
            	*end = temp;
           	 start++;
           	 end--;
        	}
    }
    
    Example
    
    void reverseString(char *str)
    {
     	   char *start = str;
       	 char *end = str + strlen(str) - 1;
       	 while(start < end){
        	    char temp = *start;
          	  *start-- = *end;
         	   *end++ = temp;
     	   }
    }
    
    Example
    
    void reverseString(char *str)
    {
        char *start = str;
        char *end = str + strlen(str) - 1;
        while (start < end){
            char temp = *start;
            *start = *end;
            *end = temp;
            start--;
            end++;
        }
    }
    

Explanation:

The accurate choice is option a. This option effectively applies pointer arithmetic to invert the string by interchanging characters from the beginning to the end while progressing towards the center.

Example

int len = strlen(str);
for(i=0;i<len/2;i++)
{
    char temp = str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
Example

int len = strlen(str);
for(i=0;i<len;i++)
{
    char temp = str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
Example

int len = strlen(str);
for(i=0;i<len/2;i++)
{
str[i]=str[len-i-1];
}
Example

int len = strlen(str);
for(i=len-1;i>-0;i--)
{
printf(?%c?, str[i]);
}

Explanation:

The correct answer is option (a). It accurately reverses the string by swapping characters at the beginning with characters at the end and moving in the direction of the center.

  1. What is the primary purpose of the strlen function in string reversal?
  • To copy the string.
  • To compare two strings.
  • To determine the length of the string.
  • To concatenate two strings.

Explanation:

The accurate choice is option c. The strlen function provides the string's length, aiding in locating the string's midpoint for various operations.

Input Required

This code uses input(). Please provide values below: