C string question

Posted by user208454 on Ask Ubuntu See other posts from Ask Ubuntu or by user208454
Published on 2013-10-27T21:42:39Z Indexed on 2013/10/27 22:01 UTC
Read the original article Hit count: 487

Filed under:
|
|

I am writing a simple c program which reverses a string, taking the string from argv[1]. Here is the code:

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

char* flip_string(char *string){
  int i = strlen(string);
  int j = 0;
  // Doesn't really matter all I wanted was the same size string for temp.
  char* temp = string; 
  puts("This is the original string");
  puts(string);
  puts("This is the \"temp\" string");
  puts(temp);

  for(i; i>=0; i--){
    temp[j] = string[i]
    if (j <= strlen(string)) {
      j++;
    }
  }

  return(temp);
}

int main(int argc, char *argv[]){
  puts(flip_string(argv[1]));
  printf("This is the end of the program\n");
}

That's basically it, the program compiles and everything but does not return the temp string in the end (just blank space). In the beginning it prints temp fine when its equal to string. Furthermore if I do a character by character printf of temp in the for loop the correct temp string in printed i.e. string -> reversed. just when I try to print it to standard out (after the for loop/ or in the main) nothing happens only blank space is printed.

© Ask Ubuntu or respective owner

Related posts about programming

Related posts about c