A
arnuld
i am not able to make it work. it compiles without any error but does
not work:
what i WANTED:
1.) 1st we will take the input in to an array. (calling "getline" in
"main")
2.) we will print that input array on terminal. (in "main")
3.) we will reverse the array. (calling "reverse" in "main")
4.) we will print that reversed array. (in "main")
what i GET:
rather than printing the reversed array, it prints an "unprintable
character" on the screen:
------------- PROGRAMME ------------------
/* K&R2: 1.9 Arrays, exercise 1-19
STATEMENT:
write a function reverse(s) that reverses the character string s.
use it to write a programme that revereses its input a line at a time.
*/
#include<stdio.h>
#define MAXLENGTH 1000
int getline(char arr[]);
void reverse(char arr[]);
int main(void)
{
int i = 0;
char arr[MAXLENGTH - 1];
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;
while(getline(arr))
{
printf("\n---------------\n%s", arr);
reverse(arr);
printf("\n ****** \n%s\n", arr);
}
return 0;
}
int getline(char arr[])
{
int c = 0;
int i = 0;
for(i=0; 9 < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n'; +
+i)
arr = c;
if(c == '\n')
arr[i++] = c;
arr = '\0';
return i;
}
void reverse(char arr[])
{
int i = 0;
int j = MAXLENGTH - 1;
char rev_arr[MAXLENGTH - 1];
while(i < MAXLENGTH - 1)
rev_arr[i++] = arr[j--];
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = rev_arr;
}
-------------- OUTPUT --------------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-19.c
[arch@voodo kr2]$ ./a.out
like this
---------------
like this
******
?
[arch@voodo kr2]$
not work:
what i WANTED:
1.) 1st we will take the input in to an array. (calling "getline" in
"main")
2.) we will print that input array on terminal. (in "main")
3.) we will reverse the array. (calling "reverse" in "main")
4.) we will print that reversed array. (in "main")
what i GET:
rather than printing the reversed array, it prints an "unprintable
character" on the screen:
------------- PROGRAMME ------------------
/* K&R2: 1.9 Arrays, exercise 1-19
STATEMENT:
write a function reverse(s) that reverses the character string s.
use it to write a programme that revereses its input a line at a time.
*/
#include<stdio.h>
#define MAXLENGTH 1000
int getline(char arr[]);
void reverse(char arr[]);
int main(void)
{
int i = 0;
char arr[MAXLENGTH - 1];
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = 0;
while(getline(arr))
{
printf("\n---------------\n%s", arr);
reverse(arr);
printf("\n ****** \n%s\n", arr);
}
return 0;
}
int getline(char arr[])
{
int c = 0;
int i = 0;
for(i=0; 9 < MAXLENGTH - 1 && (c = getchar()) != EOF && c != '\n'; +
+i)
arr = c;
if(c == '\n')
arr[i++] = c;
arr = '\0';
return i;
}
void reverse(char arr[])
{
int i = 0;
int j = MAXLENGTH - 1;
char rev_arr[MAXLENGTH - 1];
while(i < MAXLENGTH - 1)
rev_arr[i++] = arr[j--];
for(i = 0; i < MAXLENGTH - 1; ++i)
arr = rev_arr;
}
-------------- OUTPUT --------------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-19.c
[arch@voodo kr2]$ ./a.out
like this
---------------
like this
******
?
[arch@voodo kr2]$