trying not to use strrev() function

This is a discussion on trying not to use strrev() function within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to create a function to reverse a string, instead of using the strrev() function supplied by the ...

  1. #1
    Unregistered
    Guest

    Question trying not to use strrev() function

    Hi,

    I'm trying to create a function to reverse a string, instead of using the strrev() function supplied by the <string.h> library.

    any ideas ??

    Thanks

    #include<stdio.h>
    #include<string.h>
    #define MAX 20 //maximum number of characters
    #define NULL_CHAR '\0' //signals end of the string

    int GETSTR(char buf[], int);
    void PUTSTR(char str[] ,int);
    void REVSTR(char str[] ,int);

    int main(void)
    {
    char text[MAX+1];
    int strlen;
    int maxchar=MAX;

    puts("Enter text up to 20 characters, press enter when done");
    strlen=GETSTR(text,maxchar);
    printf("the number of character(s) is %d\n",strlen);
    printf("the reversed text is: %c\n");
    REVSTR(text, strlen);
    PUTSTR(text,strlen);
    printf("\n");

    return(0);
    }

    //name:Getstr input a string from the standard input
    //synopsis:int Getstr(char buf[],int maxlen)
    //arguments:buf input/output buffer
    // maxlen maximum length ofstring to read
    //returns:the number of characters in the string
    //description:this functions gets line of input ended by a new line or
    //eof.if the input is larger than maxlen,only up to size of the maxlen
    //will be returned.
    //the input buffer must be big enough for maxlen bytes,and null terminator

    int GETSTR(char buf[],int maxlen)
    {
    char ch;
    int countch=0;

    while((ch=getchar()) !='\n'&& ch!=EOF && countch<=maxlen)
    {
    buf[countch++]=ch;
    }
    buf[countch]=NULL_CHAR;
    return(countch);
    }
    //name:Putstr output a string to the standard output
    //synopsis:void Putstr(char str[],int len);
    //arguments:str[] buffer of the input string that stores string characters
    // len length of input string
    //returns:none
    //description:this function prints len characters from str[]

    void PUTSTR(char str[],int len)
    {
    int index=0;

    while((index<len)&&(str[index] !='\0')){
    putchar(str[index]);
    index++;
    }
    }

    void REVSTR(char str[], int length)
    {
    char my_str[20];
    char new_str[20];
    int i, j;

    for (i = 0, j = strlen(my_str) - 1; j >= 0; i++, j--)

    new_str[i] = my_str[j];
    new_str[i] = NULL_CHAR;


    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    void Reverse(char* pData)
    
    {
    	int Length=strlen(pData);
                    char temp; //holding area
    	char* last=pData+Length-1;//last char before null
    	while (pData<last) // stops at midpoint
    		{
    			temp=*pData;
    			*pData=*last;
    			*last=temp;
    			++pData;
    			--last;
    		}
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > *last=temp;

    What exactly is the point of this line? I see when you are setting the pointers and incrementing/decrementing and I understand that, but I don't understand why you would want to make *last = temp. Why?

    --Garfield

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Damm beaten again

    Code:
    void REVSTR(char *source_ptr, char *target_ptr)
    {
    	int x, size;
    
    	size = strlen(source_ptr) - 1;
    	source_ptr += size;
    
    	for(x = 0; x <= size; x++, source_ptr--, target_ptr++)
    		*target_ptr = *source_ptr;
    
    	*target_ptr = '\0';
    
    	return;
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Originally posted by Garfield
    > *last=temp;

    What exactly is the point of this line? I see when you are setting the pointers and incrementing/decrementing and I understand that, but I don't understand why you would want to make *last = temp. Why?

    --Garfield
    As the function is moving characters, to prevent a character being overwritten before it's been copied it's stored in a temporary variable. Although, it is possible to sort/move elements of an array without using a temporary variable -

    Code:
    void Reverse(char* pData)
    {
    	int Length=strlen(pData);                
    	char* last=pData+Length-1;//last char before null
    	while (pData<last) // stops at midpoint
    		{
    			*pData^=*last;
    			*last^=*pData;
    			*pData^=*last;
    			++pData;
    			--last;
    			
    		}
    }
    zen

  6. #6
    Unregistered
    Guest

    Talking

    I would just like to say thank you for all your valuable assistance it's greatly appreciated. Everybody who replied your the best.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21