Wednesday , 22 October 2014
Home » Technical Interview » How to reverse a C-Style String Interview Question

How to reverse a C-Style String Interview Question

Q. Write code to reverse a C-Style String  (C-String means that “abcd” is represented as five characters, including the null character )

A.)

This is a classic interview question   The only “gotcha” is to try to do it in place, and to be care-ful for the null character

void reverse(char *str) {
   char * end = str;
   char tmp;
   if (str) {
     while (*end) {
       ++end;
     }
     --end;
     while (str < end) {
        tmp = *str;
        *str++ = *end;
        *end-- = tmp;
      }
    }
  }

About admin

Let me first introduce myself.... Sudhir has done my Engineering in Information Technology form Lingaya's Institute of Management and Technology Faridabad, India. Sudhir has worked on VC++, MFC, COM, C++, C Sharp, ASP.NET, Sharepoint and on Latest Web 2.0 stuff Jquery, Ajax. Currently Sudhir Mangla is working as Team Leader (architect ) (Silverlight , C Sharp, ASP.NET, Sharepoint and on Latest Web 2.0 stuff Jquery, Ajax) developer in Svam International, Noida.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>