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;
}