Results 1 to 1 of 1

Thread: *RESOLVED* It prints the null char too. Why?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21

    *RESOLVED* It prints the null char too. Why?

    PLEASE IGNORE. SORRY! FIGURED IT OUT! FEELING LIKE AN IDIOT.

    Just practicing. My StrReverse works like a charm but the only complaint is that it prints the null char as well on the screen. Why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char* StrReverse(char*);
    
    
    int main(void)
    {
    	char* str;
    	char* ptr;
    	char cArray[] = "Hello, how are you?";
    
    	str=cArray;
    	ptr=0;
    
    	ptr = StrReverse(str);
    	if (ptr) printf("%s\n",ptr);
    }
    
    
    char* StrReverse(char* str)
    {
    	char* temp;
    	int i =0,j =0;
    
    	i=strlen(str);
    
    	temp = (char*)malloc((i+1)* sizeof(char));
    	
    	if (temp)
    	{
    		for(j=0;j<i;j++)
    			*(temp+j) = *(str+i-j-1);
    		
    		*(temp+j)='\0';
    	}
    	return temp;
    }
    Last edited by estudiantin; Jun 19th, 2004 at 01:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width