T
Tiro Verus
Recently I was seized by the desire to "rotate" the following text:
agdtk
laeha
pmlep
hmttp
aaaaa
into this:
alpha
gamma
delta
theta
kappa
After several detours, some of which were beset by sundry glitchings,
this is what I have:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char * app_char (char c);
int main (int argc, char *argv[]) {
/* "rotate" vertically transcribed words to horizontal ones */
/* using a append_char function */
FILE *ifp; FILE *ofp;
char *line;
char *letters[5];
int i; int j;
char * temp = malloc (2 * sizeof(char));
char c = 'i';
int nrows = 6; int ncols = 6;
char **wordlist = (char **) malloc( nrows * sizeof(char *));
for (i = 0; i < 7; i++ ) {
wordlist = (char *)malloc (ncols *sizeof(char));
}
if ( ((ifp = fopen (argv[1], "r")) == NULL) ||
((ofp = fopen (argv[2], "w")) == NULL) ) {
printf ("can\'t open file\n");
exit (1);
}
while (!feof(ifp)) {
if (fscanf(ifp, "%s", line) != 1) {
break; }
printf ("the first letter in %s is %s\n", line, app_char(line[0]));
for (j = 0; j < 5; j++) {
temp = app_char(line[j]);
strcat (wordlist[j], temp);
}
} /* end of feof/fscanf loop */
/* the following is not C it was */
/* */
/* for $j (0..4) { */
/* $newlist[$j] .= $mline[$j]; */
/* */
/* how I arrived at the solution */
/* terminate the strings */
for (i =0; i < 5; i++) {
wordlist[6] = '\0';
}
for (j = 0; j < 5; j++) {
printf (" %s \n", wordlist[j]);
}
fclose (ifp); fclose (ifp);
return 0;
}
char * app_char (char c) {
char h = c;
char * twochar = malloc (2 * sizeof (char));
twochar[0] = h;
twochar[1] = '\0';
return twochar;
}
Any ideas, criticisms, etc. invited. Regards T.
agdtk
laeha
pmlep
hmttp
aaaaa
into this:
alpha
gamma
delta
theta
kappa
After several detours, some of which were beset by sundry glitchings,
this is what I have:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char * app_char (char c);
int main (int argc, char *argv[]) {
/* "rotate" vertically transcribed words to horizontal ones */
/* using a append_char function */
FILE *ifp; FILE *ofp;
char *line;
char *letters[5];
int i; int j;
char * temp = malloc (2 * sizeof(char));
char c = 'i';
int nrows = 6; int ncols = 6;
char **wordlist = (char **) malloc( nrows * sizeof(char *));
for (i = 0; i < 7; i++ ) {
wordlist = (char *)malloc (ncols *sizeof(char));
}
if ( ((ifp = fopen (argv[1], "r")) == NULL) ||
((ofp = fopen (argv[2], "w")) == NULL) ) {
printf ("can\'t open file\n");
exit (1);
}
while (!feof(ifp)) {
if (fscanf(ifp, "%s", line) != 1) {
break; }
printf ("the first letter in %s is %s\n", line, app_char(line[0]));
for (j = 0; j < 5; j++) {
temp = app_char(line[j]);
strcat (wordlist[j], temp);
}
} /* end of feof/fscanf loop */
/* the following is not C it was */
/* */
/* for $j (0..4) { */
/* $newlist[$j] .= $mline[$j]; */
/* */
/* how I arrived at the solution */
/* terminate the strings */
for (i =0; i < 5; i++) {
wordlist[6] = '\0';
}
for (j = 0; j < 5; j++) {
printf (" %s \n", wordlist[j]);
}
fclose (ifp); fclose (ifp);
return 0;
}
char * app_char (char c) {
char h = c;
char * twochar = malloc (2 * sizeof (char));
twochar[0] = h;
twochar[1] = '\0';
return twochar;
}
Any ideas, criticisms, etc. invited. Regards T.