Reverse String Stored in Pointer Array

Write a C program to reverse the strings stored in the following array of pointers to strings

char *s[]={"To err is human..",
"But to really mess things up",
"One needs to learn C!!"
};

Questions by vishv

Showing Answers 1 - 12 of 12 Answers

pranith

  • Dec 19th, 2010
 

#include<stdio.h>
#include<string.h>
int main()
{
    char *s[]={"To err is human..","But to really mess things up","One needs to learn C!!"};
    char *buf;
    buf=strrev(s[0]);
    printf("%st",buf);
    buf=strrev(s[1]);
    printf("%st",buf);
    buf=strrev(s[2]);
    printf("%st",buf);
    return(0);
}

  Was this answer useful?  Yes

maddynator

  • Dec 23rd, 2010
 

A better way is to use recursion

main() {
char *str[]= {"abc", "def", "ghi"};
int length = sizeof (str)/ sizeof *str;
int i =0;
while ( i< length) {
reverse(str[i]);
}

char reverse (char *str1) {
while (*str1 !=NULL) {
reverse(str1 + 1);
print("%s", str1);
}

  Was this answer useful?  Yes

#include <stdio.h>
#include <string.h>

void reverse (char *);

main()
{
 char *s[] = {
     "To err is human...",
     "But to really mess things up...",
     "One needs to know C!!"
    };
 int i;
 for(i=0;i<3;i++)
 {
  reverse(s[i]);
  printf("%sn",s[i]);
 }
}

void reverse (char *s)
{
 char *p,temp;
 int length=strlen(s);
 p = s+length-1;
 int i;
 for(i=0;i<(length/2);i++)
 {
  temp = *p;
  *p=*s;
  *s=temp;
  s++;
  p--;
 }
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions