A
arnuld
i have created a solutions myself. it compiles without any trouble
and runs but it prints some strange characters. i am not able to find
where is the trouble.
--------------------------------- PROGRAMME
--------------------------------
/* K&R2 section 1.9
exercise 1.19
STATEMENT: write a function "reverse(s)" that reverses the character
string "s". use it to to write a programme that reverses
its input a line at a time.
it works like this:
1.) "main" takes input from user and creates a char array from that.
2.) then "main" calls "reverse_them" to reverse the char array it
created.
3.) "reverse_them", 1st, calls "arr_size" on the array "main" created.
4.) 2nd, "reverse them", then revrses the characters of 1st array by
putting
the array into in a 2nd array(a new array) in reverse order.
*/
#include <stdio.h>
#define MAXLINE 1000
int arr_size(char arr[]);
void reverse_them(char to[], char from[]);
int main(void) {
char my_array[MAXLINE];
char reversed_array[MAXLINE];
int i, c;
/* now we will take some input and feed it into array */
for(i = 0; ((c = getchar()) != EOF) && i < MAXLINE; ++i)
my_array = c;
my_array = '\0'; /* '\0' is required in the end */
printf("array size: %d\n", arr_size(my_array));
reverse_them(my_array, reversed_array);
return 0;
}
/* this function "reverses:" the 1st array into 2nd */
void reverse_them(char from[], char to[])
{
int i, asize, li;
asize = arr_size(from); /* get input array size */
li = asize - 1; /* because of last '\0' element */
for(i = 0; (to[li] = from) != '\0'; ++i)
--li;
printf("\nthe original input: %s\n", from);
printf("reversed input: %s\n", to);
}
/* this function gets the size of an array */
int arr_size(char ar[])
{
int i;
for(i = 0; (ar != '\0') == 1; ++i)
;
return i;
}
------------------------------- OUTPUT -----------------------
[arch@voodo kr2]$ ./a.out
like this
K&R2
array size: 17
the original input: like this
K&R2
reversed input:
2R&K
siht ekil???
[arch@voodo kr2]$
and runs but it prints some strange characters. i am not able to find
where is the trouble.
--------------------------------- PROGRAMME
--------------------------------
/* K&R2 section 1.9
exercise 1.19
STATEMENT: write a function "reverse(s)" that reverses the character
string "s". use it to to write a programme that reverses
its input a line at a time.
it works like this:
1.) "main" takes input from user and creates a char array from that.
2.) then "main" calls "reverse_them" to reverse the char array it
created.
3.) "reverse_them", 1st, calls "arr_size" on the array "main" created.
4.) 2nd, "reverse them", then revrses the characters of 1st array by
putting
the array into in a 2nd array(a new array) in reverse order.
*/
#include <stdio.h>
#define MAXLINE 1000
int arr_size(char arr[]);
void reverse_them(char to[], char from[]);
int main(void) {
char my_array[MAXLINE];
char reversed_array[MAXLINE];
int i, c;
/* now we will take some input and feed it into array */
for(i = 0; ((c = getchar()) != EOF) && i < MAXLINE; ++i)
my_array = c;
my_array = '\0'; /* '\0' is required in the end */
printf("array size: %d\n", arr_size(my_array));
reverse_them(my_array, reversed_array);
return 0;
}
/* this function "reverses:" the 1st array into 2nd */
void reverse_them(char from[], char to[])
{
int i, asize, li;
asize = arr_size(from); /* get input array size */
li = asize - 1; /* because of last '\0' element */
for(i = 0; (to[li] = from) != '\0'; ++i)
--li;
printf("\nthe original input: %s\n", from);
printf("reversed input: %s\n", to);
}
/* this function gets the size of an array */
int arr_size(char ar[])
{
int i;
for(i = 0; (ar != '\0') == 1; ++i)
;
return i;
}
------------------------------- OUTPUT -----------------------
[arch@voodo kr2]$ ./a.out
like this
K&R2
array size: 17
the original input: like this
K&R2
reversed input:
2R&K
siht ekil???
[arch@voodo kr2]$