- #1
gruba
- 206
- 1
Homework Statement
Write a program that reads strings(words) from a text file and copy the inverted words to another file (use the command line arguments). Form a new array of words b from original array a using the function
void formNewArray(char **a, char **b, int n, char* (*t)(char *));
where n is the number of words in array, and (*t) is the pointer to function for inverting a string.
Homework Equations
-File handling
-Pointers
The Attempt at a Solution
The following program gives SIGSEGV segmentation fault in function formNewArray():
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void form_temp_array(char **a,char **b,int n,char* (*t)(char *))
{
int i;
for(i=0 ;i<n; i++)
b[i]=(*t)(a[i]);
}
char* reverseWord(char *word)
{
char *new_word;
new_word=(char*)malloc(sizeof(strlen(word+1)));
new_word=strrev(word);
return new_word;
}
void print(char **arr,int n)
{
int i;
for(i=0; i<n; i++)
printf("%s\n",arr[i]);
}
int main()
{
char* a[]={"PROGRAMMING","CODING"};
int n=sizeof(a)/sizeof(char**);
char* b[n];
printf("original words:\n");
print(a,n);
printf("reversed words:\n");
form_temp_array(a,b,n,&reverseWord);
print(b,n);
free(reverseWord);
return 0;
}
Last edited: