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

void main() {
    int i, j, k=0;
    int len;
    char *rev[3];
    char *s[]={
        "To err is human...",
        "But to really mess things up...",
        "One needs to know c!!"
    };
    for (i=0; i<3; i++) {
        len=strlen(s[i]);
        rev[i]=malloc(strlen(s[i])+1);
        for (j=len-1; j>=0; j--) {
            rev[i][k]=s[i][j];
            k++;    
        }
        rev[i][k]='\0';    
    }
    for (i=0; i<3; i++)
        printf("%s\n", rev[i]);
}

This program compiles fine but does not run. Can anyone point out the logical error or conceptual? I'm trying to reverse the string here...

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    Please indent the code correctly. – dornhege Jan 22 '14 at 14:27
  • `main` **must** return `int` – Fiddling Bits Jan 22 '14 at 14:29
  • @FiddlingBits Not correct. [Read this](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593) – Lundin Jan 22 '14 at 14:30
  • @FiddlingBits---why??....It is not neccessary. –  Jan 22 '14 at 14:31
  • @dornhege----how do i do that??? –  Jan 22 '14 at 14:32
  • @Lundin I'm assuming OP is not running this on an embedded device. – Fiddling Bits Jan 22 '14 at 14:34
  • @dividedbyzero360 It is necessary if you are compiling your code for a hosted system, such as PC, Linux, Mac, Android. Then main _must_ return `int`. If you are compiling code for a freestanding system, such as embedded microcontrollers, or if you are making an OS, then the return type of main can be anything. – Lundin Jan 22 '14 at 14:34
  • @FiddlingBits Why? There is nothing in the question indicating this. You should not assume that everyone is sitting behind a Windows PC, unless they clearly state that they do. – Lundin Jan 22 '14 at 14:35
  • @Lundin The book i use "Let US C" doesn't say anything about it. And all my codes with "void main()" runs fine.Is it really necessary to use int?I'm programming on my windows 7 machine –  Jan 22 '14 at 14:38
  • @Lundin If you don't make certain assumptions, your answer will have to cover every possibility, no? – Fiddling Bits Jan 22 '14 at 14:39
  • @dividedbyzero360 If the book tells you to use `void main` without stating why, then it is a bad book. It is necessary to use int. If your code compiles when you use `void`, then your compiler is likely using some non-standard extension of C. That is not a good thing when learning: you should be learning the C language, not some home-brewed, compiler-specific language which isn't C. – Lundin Jan 22 '14 at 15:13
  • @FiddlingBits Why? As long as the answer to a question tagged C sticks to pure C standard code, you don't need to assume anything about the specific system. – Lundin Jan 22 '14 at 15:16
  • @Lundin Sure sir.I will keep that is mind.Sir, can you recommend me any good books on programming and developing logic? –  Jan 22 '14 at 15:57

2 Answers2

2

The problem appears to be that you don't reset k to 0 for each string (only at the beginning of the program). So you should have k = 0; before the inner for loop.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

While you've already gotten an answer that makes the code work, I'd recommend going about things a bit differently. I'd move the code to reverse one string into a separate function:

char *reverse(char const *in) {
    char *ret;
    int j, k=0;
    size_t len=strlen(in);
    ret=malloc(len+1);
    for (j=len-1; j>=0; j--) {
        ret[k]=in[j];
        k++;
    }
    ret[k]='\0';
    return ret;
}

Then I'd call that from main to reverse strings as needed:

for (i=0; i<3; i++) 
    rev[i]=reverse(s[i]);

Also keep in mind that the strings in rev were allocated with malloc so you should free them when you don't need them any more:

for (i=0; i<3; i++) {
    printf("%s\n", rev[i]);
    free(rev[i]);
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • ...Thank you sir...and the free() thing...i will surely keep that in mind –  Jan 22 '14 at 15:59