I have an assignment to print a perfect square depending on two inputs one of them is supposed to be the row and the other one should be the columns.
example:
if the input is (5,3) it should print out this
o---o
| |
o---o
I have this code that does the job until my last test which is the input seems to be empty [] and I get the error
SIGSEGV (signal 11)
The code is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *array;
size_t used;
size_t size;
} StringArray;
void initArray(StringArray *a, size_t initialSize) {
a->array = malloc(initialSize * sizeof(char));
a->used = 0;
a->size = initialSize;
}
void insertArray(StringArray *a, char element) {
if (a->used == a->size) {
a->size *= 2; // if reach limit duplicate size and alloc in the heap
a->array = realloc(a->array, a->size * sizeof(char));
}
a->array[a->used++] = element;
}
void clearArray(StringArray *a, size_t newSize) {
a->array = NULL;
a->used = 0;
a->array = realloc(a->array, newSize * sizeof(char));
a->size = newSize;
}
void freeArray(StringArray *a) {
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
void printLineX( int x, StringArray line){
for (int i = 0; i < x; i++) {
if(i == 0 || i == x-1)insertArray(&line, 'o');
else insertArray(&line, '-');
}
printf("%s\n", line.array);
//
}
void printLineY(int x, StringArray line){
for (int i = 0; i < x; i++) {
if(( i == 0) || ( i == x-1))insertArray(&line, '|');
else insertArray(&line, ' ');
}
printf("%s\n", line.array);
// freeArray(&line);
}
int main(int argc, char **argv) {
// remove 1 from each dimension to offset for first char
int x;
int y;
if(argc < 3) {
x = atoi(argv[1]);
y = 1;
}
else if (argc < 2){
x = 1;
y = 1;
}
else{
x = atoi(argv[1]);
y = atoi(argv[2]);
}
if (x < 1) x = 0;
if (y < 1) y = 0;
StringArray line;
initArray(&line, x);
if(argc > 2)
for (int i = 0; i < y; i++) {
if(i == 0 || i == y -1) printLineX( x, line);
else printLineY( x, line);
clearArray(&line, x);
}
freeArray(&line);
return 0;
}
if(argc < 3) {
x = atoi(argv[1]);
y = 1;
}
else if (argc < 2){
x = 1;
y = 1;
}
This part is wrong. argc < 3 is true when argc is 1, so this code may access argv[1] even if it is empty.
It should be:
if (argc < 2){
x = 1;
y = 1;
}
else if(argc < 3) {
x = atoi(argv[1]);
y = 1;
}
Related
i have to sort my arrays of record but when compare 2 record one of them has NULL value. At this moment my debug for C doesn't work so if someone could give me a link to show me "how to" use , cause make error when launch with "PreLaunch returned -1 exit status" and after theh "file doesn't exixt in the path"...but this was an offtopic,so..i leave my code below , maybe i'm wrong with the usage of pointers to function cause i tried to print every call of the function " compare" and show me numeber more time that i aspected.
Main:
#include "ordered_array.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define START_CAPACITY_RECORD 10
typedef struct {
int id;
char* field1;
int field2;
float field3;
}Record;
int compare_record_int(void* ele1, void* ele2){
if(ele1 == NULL){
fprintf(stderr,"precedes_record_int_field: the first parameter is a null pointer");
exit(EXIT_FAILURE);
}
if(ele2 == NULL){
fprintf(stderr,"precedes_record_int_field: the second parameter is a null pointer");
exit(EXIT_FAILURE);
}
Record *A = (Record*)ele1;printf("\n\nele1: %d",A->id);
Record *B = (Record*)ele2;printf("\nele2: %d\n",B->id);
if(A->id < B->id) return 1;
else if(A->id > B->id)return 2;
return 0;
}
int GetRecord(char* file_name,Record* A){
FILE *fd = fopen(file_name,"rt");int i=0;
char* st=(char*)malloc(100*sizeof(char*));
if(fd == NULL){
printf("GetRecord:path file doesn't exist");
exit(EXIT_FAILURE);
};
while(!feof(fd)){
fscanf(fd,"%s\n",st);
A[i].id=atoi(strtok(st, ";"));
A[i].field1 = strdup(strtok(NULL, ";"));
A[i].field2 = atoi(strtok(NULL, ";"));
A[i].field3 = atof(strtok(NULL, ";"));
i++;
}
fclose(fd);
return i;
}
void print_record(void* record){
Record *B = (Record*)record;
printf("\nValore %d field1 %s field2 %i field3 %f ",B->id,B->field1,B->field2,B->field3);
}
int main(int argc,char* argv[]){
OrderedArray* b = ordered_array_create();
Record* A = (Record*)malloc(START_CAPACITY_RECORD*sizeof(Record));
int dim = GetRecord("TEST.csv",A);
for(int i = 0; i<dim;i++){
ordered_array_add(b,&A[i]);
}
ordered_array_print(b,print_record);
ordered_array_sort_by(b,compare_record_int);
ordered_array_print(b,print_record);
ordered_array_free(b);
free(A);
}
Library(only the part with the sort methods,for others ask me to edit in the comment):
void ordered_array_sort_by(OrderedArray * a, int (*compare)(void*,void*)){
if(a== NULL){
fprintf(stderr,"ordered_array_sort_by: ordered_array parameter cannot be NULL");
exit(EXIT_FAILURE);
}
if(compare == NULL){
fprintf(stderr,"ordered_array_sort_by: function parameter cannot be NULL");
exit(EXIT_FAILURE);
}
a->compare = compare;
a->array = array_merge_binary_insertion(a);
}
void** array_merge_binary_insertion(OrderedArray *a){
if (ordered_array_size(a) == 1)return a->array;
int m = ordered_array_size(a) / 2;
OrderedArray *B = ordered_array_create(m);
for(int i=0;i<m;i++)ordered_array_add(B,a->array[i]);
OrderedArray *C = ordered_array_create(m);
for(int i=m;i<ordered_array_size(a);i++)ordered_array_add(C,a->array[i]);
if (m <= 10){
B->array = array_binary_insertion(B->array, ordered_array_size(B),a->compare);
C->array = array_binary_insertion(C->array, ordered_array_size(C),a->compare);
}else{
B->array = array_merge_binary_insertion(B);
C->array = array_merge_binary_insertion(C);
}
return array_merge(B->array, C->array,a->compare);
}
void** array_merge(void** B,void** C,int (*compare)(void*,void*)){
if (B == NULL) return C;
else if (C == NULL) return B;
int dimB = sizeof(B) + 1;
int dimC = sizeof(C) + 1;
OrderedArray *X = ordered_array_create(dimB+dimC);
int x = 0, y = 0, i = 0;
while ((i < dimB) || (x < dimC)){
if((B[i]==NULL)||(C[i]==NULL))printf("\nerrore");
if ((*compare)(B[i],C[x]) == 1){
X->array[y] = B[i];
i++;
} else{
X->array[y] = C[x];
x++;
}
y++;
}
return X->array;
}
void** array_binary_insertion(void** array,int size,int (*compare)(void*,void*)){
//ricontrollare la funzione, non sistema bene
if(size == 1)return array;
int j = 0, x, y, temp;
OrderedArray *temp_array = ordered_array_create(size);
temp_array->array[0] = array[0];
for (int i = 1; i < size; i++){
temp = (int)array[i];
x = array_binary_search(temp_array->array, temp, 0, j,compare);
y = j;
while (y >= x){
temp_array->array[y + 1] = temp_array->array[y];
y--;
}
temp_array->array[x] = (void*)temp;
j++;
}
return temp_array->array;
}
int array_binary_search(void** array,int a,int l,int h,int (*compare)(void*,void*)){
if((array[l]==NULL)||((void*)a==NULL))printf("\nerrore");
if (l >= h){
if ((*compare)((void*)a,array[l]) == 2)
return l + 1;
else
return l;
}
int m = (l + h) / 2;
if ((*compare)((void*)a,array[m]) == 0)
return m + 1;
else if ((*compare)((void*)a,array[m]) == 2)
return array_binary_search(array, a, m + 1, h,compare);
else
return array_binary_search(array, a, l, m - 1,compare);
}
Meanwhile if you have other advice would be gratefully accepted.
So far I have the program does what it needs to do. My issue now is that I have two arrays and when I print them I get 0s for empty elements. I want the empty elements to print nothing.
Example:
Array 1:
1 1
Array 2:
3 3 3 3
Output:
1 3 1 3 0 3 0 3
My goal is:
1 3 1 3 3 3
which is to remove the 0s if I didnt input 0 in array
My code:
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
char * line = NULL;
size_t len = 0;
char * line2 = NULL;
size_t len2 = 0;
char ch;
int counter = 0;
char ch2;
int counter2 = 0;
int mSize;
int mergedArray[20];
int i = 0; // for loop
int j = 0;
int * myPtr;
myPtr = (int * ) malloc(counter * sizeof(int));
int * myPtr2;
myPtr2 = (int * ) malloc(counter2 * sizeof(int));
while (getline( & line, & len, stdin) != EOF) {
//===============
//value 1 for line 1
ch = * line;
printf("line 1: Test: %s\n", line);
char * start = line;
char * eon;
long value;
//===============
//value 2 line 2
getline( & line2, & len2, stdin);
ch2 = * line2;
printf("line 2: Test: %s\n", line2);
char * start2 = line2;
char * eon2;
long value2;
//==============
errno = 0;
//============loop for line 1 =================
printf("=============\n");
printf("Line 1\n");
while ((value = strtol(start, & eon, 0)),
eon != start &&
!((errno == EINVAL && value == 0) ||
(errno == ERANGE && (value == LONG_MIN || value == LONG_MAX))))
{
//getting the size of the line
counter++;
start = eon;
errno = 0;
myPtr[counter] = value;
// printf("Array #1 [%d] %d\n",counter , myPtr[counter]);
} //end of while
printf("Size: %d\n", counter);
printf("=============\n");
//============loop for line 2 =================
printf("Line 2\n");
while ((value2 = strtol(start2, & eon2, 0)),
eon2 != start2 &&
!((errno == EINVAL && value2 == 0) ||
(errno == ERANGE && (value2 == LONG_MIN || value2 == LONG_MAX))))
{
//getting the size of the line
counter2++;
start2 = eon2;
errno = 0;
myPtr2[counter2] = value2;
//printf("Array #2 [%d] %d\n",counter2 , myPtr2[counter2]);
} //end of while
printf("Size: %d\n", counter2);
printf("=============\n");
for (i = 0; i < counter; i++) {
mergedArray[i] = myPtr[i + 1]; // not used
}
mSize = counter + counter2;
for (i = 0, j = counter; j < mSize && i < counter2; i++, j++) {
mergedArray[j] = myPtr2[i + 1]; // not used
//here I print out both arrays
printf("%d %d ", myPtr[i + 1], myPtr2[i + 1]);
}
} // end of main while
return 0;
}
Your program is extremelly complicated. It is a good practice to separate login into functions. In the solution below you need to provide large enouth array for the destination array and both arrays to be merged.
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define SA(a) (sizeof(a)/sizeof((a)[0]))
size_t mergeArrays(int *dest, const int *src1, const int *src2, size_t size_1, size_t size_2)
{
size_t pos = 0;
for(size_t index = 0; index < MAX(size_1, size_2); index++)
{
if(index < size_1) dest[pos++] = src1[index];
if(index < size_2) dest[pos++] = src2[index];
}
return pos;
}
int main(void)
{
int arr1[] = {1,1};
int arr2[] = {3,3,3,3,3};
int dest[SA(arr1) + SA(arr2)];
size_t destsize = mergeArrays(dest, arr1, arr2, SA(arr1), SA(arr2));
for(size_t index = 0; index < destsize; index++)
{
printf("%d ", dest[index]);
}
printf("\n");
}
https://godbolt.org/z/WG4v6T
Here you have the version reading from user and using dynamic memory allocation:
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define SA(a) (sizeof(a)/sizeof((a)[0]))
size_t mergeArrays(int *dest, const int *src1, const int *src2, size_t size_1, size_t size_2)
{
size_t pos = 0;
for(size_t index = 0; index < MAX(size_1, size_2); index++)
{
if(index < size_1) dest[pos++] = src1[index];
if(index < size_2) dest[pos++] = src2[index];
}
return pos;
}
int *readArray(size_t *size)
{
int *arr = NULL;
printf("Enter size:");
if(scanf(" %zu", size) != 1) goto func_return;
arr = malloc(*size * sizeof(*arr));
if(!arr) { free(arr); arr = NULL; goto func_return;}
for(size_t index = 0; index < *size; index++)
{
if(scanf(" %d", &arr[index]) != 1) {free(arr); arr = NULL; goto func_return;}
}
func_return:
return arr;
}
int main(void)
{
size_t size1, size2;
int *arr1 = readArray(&size1);
int *arr2 = readArray(&size2);
int *dest = NULL;
if(arr1 && arr2) dest = malloc(size1 + size2);
if(dest)
{
size_t destsize = mergeArrays(dest, arr1, arr2, size1, size2);
for(size_t index = 0; index < destsize; index++)
{
printf("%d ", dest[index]);
}
}
printf("\n");
free(arr1);
free(arr2);
free(dest);
}
https://godbolt.org/z/5sMbYj
I wrote a Code that reads from a txt.file stores it into an array , remove the spaces then print them out.
I want to add another functionality. This time is to check if the user provided the right input file.
I want to compare the array reds with the array stringcard and see if the array red contains all the elements of the array stringcard.
I have been searching on the internet for a while but I don't know how to solve this problem.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max 13
#define stringlength 8
const char *stringcard[] = {
"REDA",
"RED2",
"RED3",
"RED4",
"RED5",
"RED6",
"RED7",
"RED8",
"RED9",
"RED10",
"REDJ",
"REDQ",
"REDK",
};
char * removechar(char str[], int ch) {
char * cpos = str;
while ((cpos = strchr(cpos, ch))) {
strcpy(cpos, cpos + 1);
}
return str;
}
int main(int argc, char ** argv) {
char * reds[max];
int i;
FILE * file = argc > 1 ? fopen(argv[1], "r") : stdin;
if (file == NULL)
return 1;
if (argc != 2) {
printf("[ERR]");
return 0;
}
for (i = 0; i < max; i++) {
reds[i] = malloc(stringlength);
fgets(reds[i], stringlength, file);
}
for (i = 0; i < max; i++) {
printf("%s", reds[i]);
}
// removes spaces
for (i = 0; i < max; i++) {
removechar(reds[i], ' ');
}
for (i = 0; i < max; i++) {
printf("%s", reds[i]);
}
int success = 1;
size_t size = sizeof(stringcard)/sizeof(stringcard[0]);
size_t size2 = sizeof(reds)/sizeof(reds[0]);
if(size == size2)
{
for(int i = 0; i<size;i++)
{
if(strcmp(stringcard[i], reds[i]) != 0){
success = 0;
printf("nope");
break;
}
}
}
return 0;
}
Input:
RED A
RED 2
RED 3
RED 4
RED 5
RED 6
RED 7
RED 8
RED 9
RED 10
RED J
RED Q
RED K
This is prefaced by my top comments.
This should work for cards in any order:
size_t size = sizeof(stringcard) / sizeof(stringcard[0]);
size_t size2 = sizeof(reds) / sizeof(reds[0]);
int allmatch = 0;
if (size == size2) {
allmatch = 1;
for (int i = 0; i < size; ++i) {
int curmatch = 0;
const char *curcard = &stringcard[i];
for (int j = 0; j < size; ++j) {
if (strcmp(curcard, reds[j]) == 0) {
curmatch = 1;
break;
}
}
if (! curmatch) {
allmatch = 0;
break;
}
}
}
printf("RESULT: %s\n",allmatch ? "MATCH" : "NOMATCH");
UPDATE:
if we know reds is sorted compare the result of strcmp with -1 or 1 rather than 0 depending on the order of the sorted elements. If stringcard is sorted of course exchange the roles
Okay, if we assume stringcard is always sorted [or we choose to pre-sort it], we can add an early escape to the inner loop that can save a small amount of time for the failed case:
size_t size = sizeof(stringcard) / sizeof(stringcard[0]);
size_t size2 = sizeof(reds) / sizeof(reds[0]);
int allmatch = 0;
if (size == size2) {
allmatch = 1;
for (int i = 0; i < size; ++i) {
int curmatch = 0;
char *redcard = reds[i];
for (int j = 0; j < size; ++j) {
int cmpflg = strcmp(redcard,stringcard[j]);
if (cmpflg == 0) {
curmatch = 1;
break;
}
if (cmpflg > 0)
break;
}
if (! curmatch) {
allmatch = 0;
break;
}
}
}
printf("RESULT: %s\n",allmatch ? "MATCH" : "NOMATCH");
Always try to use functions when possible.
int all_match(const char **table1, const char **table2, size_t t1size, size_t t2size)
{
for(size_t t1index = 0; t1index < t1size; t1index++)
{
int match = 0;
for(size_t t2index = 0; t2index < t2size; t2index++)
{
match = match || !strcmp(table1[t1index], table2[t2index]);
if(match) break;
}
if(!match) return 0;
}
return 1;
}
Given your other array is defined similarly, say inputArray, and both arrays are sorted before executing the following, then you could use code similar to: (including number of elements
//Assumes both arrays are sorted before executing the following
char success = 1;
size_t size = sizeof(stringcard)/sizeof(stringcard[0]);
size_t size2 = sizeof(inputArray)/sizeof(inputArray[0]);
if(size == size2)
{
for(int i = 0; i<size;i++)
{
if(strcmp(stringcard[i], inputArray[i]) != 0)
{
success = 0;
break;
}
}
{
else
{
success = 0;
}
//Proceed according to the value of success,
...
I have a array of strings and I want to find the first pseudopalindrome in the array for every string (if there is any). So I decided to sort my array at first, then reverse the word and do a binary search for a reversed word. So here it is what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char len(char *x){
char len = 0;
while (*x != '\0'){
x++;
len++;
}
return len;
}
char compare(char *x, char *y){
char x0 = &x;
char y0 = &y;
while (*x != '\0'){
if (tolower(*x) < tolower(*y)) return -1;
if (tolower(*x) > tolower(*y)) return 1;
x++;
y++;
}
// if we are here it means that strings are equal (case insensitive)
x = &x0;
y = &y0;
while (*x != '\0'){
if (*x > *y) return -1;
if (*x < *y) return 1;
x++;
y++;
}
// strings are equal (case sensitive)
return 0;
}
char *reverse(char *x){
int i, j;
char temp, *rev = NULL;
rev = malloc(sizeof(char)*(len(x)+1));
rev = strcpy(rev,x);
i = 0;
j = len(x) - 1;
while (i < j){
temp = rev[i];
rev[i] = x[j];
rev[j] = temp;
i++;
j--;
}
return rev;
}
int binsearch(char *x, char *A, int len){
int l, r, m, index;
l = 0;
r = len - 1;
index = -1;
while (l <= r){
m = (l + r) / 2;
if (compare(x, A[m]) == 0){
index = m;
r = m - 1;
}
else if (compare(x, A[m]) == -1) r = m - 1;
else l = m + 1;
}
return index;
}
int main()
{
int n, i, j, k, fnd;
char T[10000][101], temp[101];
scanf("%d", &n);
for (i = 0; i < n; i++){
scanf("%s", &T[i]);
}
for (i = 1; i < n; i++){
strcpy(temp, T[i]);
j = i - 1;
while (j >= 0 && compare(T[j], temp) == 1){
strcpy(T[j+1], T[j]);
j--;
}
strcpy(T[j+1], temp);
}
for (i = 0; i < n; i++){
fnd = binsearch(reverse(T[i]), T, n);
printf("%d", fnd);
}
return 0;
}
This program stops executing. The problem is probably with binary search as every function before executes well. But what's wrong with this binary search? Or what else can break the code?
Does the return type causing the problem .. for binary search no return type is mentioned
Edit 1. But you have not mentioned return type for function in its declaration
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char len(char *x){
char len = 0;
while (*x != '\0'){
x++;
len++;
}
return len;
}
char compare(char *x, char *y){
char* x0 = x;
char* y0 = y;
while (*x != '\0'){
int a0 = tolower(*x);
int b0 = tolower(*y);
if ( a0 < b0)
return -1;
if ( a0 > b0)
return 1;
x++;
y++;
}
// if we are here it means that strings are equal (case insensitive)
x = x0;
y = y0;
while (*x != '\0'){
if (*x > *y) return -1;
if (*x < *y) return 1;
x++;
y++;
}
// strings are equal (case sensitive)
return 0;
}
char *reverse(char *x){
int i, j;
char temp, *rev = NULL;
rev = malloc(sizeof(char)*(len(x)+1));
rev = strcpy(rev,x);
i = 0;
j = len(x) - 1;
while (i < j){
temp = rev[i];
rev[i] = x[j];
rev[j] = temp;
i++;
j--;
}
return rev;
}
int binarysearch(char *x,char A[][101], int len){
int l, r, m, index;
l = 0;
r = len - 1;
index = -1;
while (l <= r){
m = (l + r) / 2;
if (compare(x, A[m]) == 0){
index = m;
r = m - 1;
}
else if (compare(x, A[m]) == -1) r = m - 1;
else l = m + 1;
}
return index;
}
int main()
{
int n, i, j, k, fnd;
char T[10000][101], temp[101];
scanf("%d", &n);
for (i = 0; i < n; i++){
scanf("%s", &T[i]);
}
for (i = 1; i < n; i++){
strcpy(temp, T[i]);
j = i - 1;
while (j >= 0 && compare(T[j], temp) == 1){
strcpy(T[j+1], T[j]);
j--;
}
strcpy(T[j+1], temp);
}
for (i = 0; i < n; i++){
fnd = binarysearch(reverse(T[i]), T, n);
printf("%d", fnd);
}
return 0;
}
Not really an answer, but code in comments is painful to read.
You may also try to simplify your code; it's quite astonishing to have some much code for just a palindrome; some functions you are reimplementing are part of standard c (strlen, strcasecmp, strdup)
The function for telling if a word is palindrome is expected to really simple; here a sample of what it could be
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
bool isspeudopalindrom(const char *x){
for (int i = 0; i < strlen(x) / 2; ++i) {
if (tolower(x[i]) != tolower(x[strlen(x) - 1 - i]))
return false;
}
return true;
}
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++){
if (isspeudopalindrom(argv[i]))
printf("palindrom\n");
else
printf("not palindrom\n");
}
return 0;
}
I'm doing a shift-reduce algorithm for our compiler design subject. This is the code.
void shiftReduce(char str[MAX_CHAR], int prodNum, int line)
{
int limit = 5, y=0;
int substrFlag = 1; //0 true 1 false
int ctr,x, counter;
int match, next;
char stack[MAX_CHAR];
clearString(stack);
OUTER:while ((strcmp(stack, prod[0].left) != 0) && (y < limit))
{
addChar(stack, str[0]);
strcpy(str, dequeue(str));
printf("Stack = %s\nQueue = %s\n", stack, str);
for (ctr = 0; ctr < prodNum; ctr++)
{
if (strstr(stack, prod[ctr].right) != NULL)
{ //substring found
substrFlag = 0;
strcpy(stack, replace(stack, prod[ctr].right, prod[ctr].left));
goto OUTER;
}
}
if ((str[0] == '\n') || (str[0] == '\0'))
y++;
}
if (strcmp(stack, prod[0].left) == 0)
;//printf("%s - Accepted.\n", stack);
else
printf("Syntax error on line %i\n", line);
}
When I comment the printf("Stack = %s\nQueue = %s\n", stack, str); line, it works well. But when I uncomment it, it returns the code 3221225477.
BTW. This is the dequeue function:
char * dequeue (char str[MAX_CHAR])
{
int x = 0; char temp;
for (x = 0; x < length(str); x++)
{
if ((x+1) < length(str))
str[x] = str[x+1];
}
return str;
}
and the addChar function:
void addChar (char * str, char letter)
{
int a = 0;
while (str[a] != '\0')
a++;
str[a] = letter;
str[a+1] = '\0';
return;
}
and finally replace function.
char * replace (char orig[MAX_CHAR], char substr[MAX_CHAR], char rep[MAX_CHAR])
{
int match, end=0, next=0;
int flag = 0; //0 true 1 false
char temp [MAX_CHAR];
char store[MAX_CHAR];
if (strstr(orig, substr) == NULL)
return NULL;
int x,y;
for (x = 0; x < length(orig); x++)
{
if (orig[x] == substr[0]) //if current character is equal to first character of substring
{
match = x;
for (y = 0; y < length(substr); y++)
{
if (orig[match+y] != substr[y])
{
flag = 1;
break;
}
}
if (flag == 0)
{
next = match + length(substr);
for (y = 0; y < length(rep); y++)
{
temp[match+y] = rep[y];
end = (match+y);
}
for (y = next; y < length(orig); y++)
{
temp[y] = orig[next+(y-next)];
}
return temp;
}
}
else
{
addChar(temp, orig[x]);
}
}
return temp;
}
PS. The prod array:
struct RULES
{
char left[MAX_CHAR];
char right[MAX_CHAR];
} RULES;
struct RULES prod[MAX_RULES];
When I comment the printf("Stack = %s\nQueue = %s\n", stack, str); line, it works well. But when I uncomment it, it returns the code 3221225477.
Then most likely either stack or str has not been 0-terminated or points to invalid memory.