Paul Flintrop Paul Flintrop - 2 months ago 18
C Question

School project segmentation fault

I need to make this program for school, however I am getting the error project terminated due to "segmentation fault"(11). I believe this has to do with a lack of memory during the program, however I don't really see a function where memory is used in extreme quantities. Any help would be appreciated.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define _CRT_SECURE_NO_WARNINGS

int STRLEN = 36;



int findOcurrance( char str[], char Char1){
for(int i = 0; i < STRLEN * 3; i++){
if(str[i] == Char1)
return i;
}
return -1;
}

void replaceVowelsA(char str[], char Char) {
for(int i = 0; i < STRLEN * 3; i++) {
int index = findOcurrance(str, Char);
str[index] = 'a';
}
}

void insertaChar(char str[], char Char1, int index){
for(int i = STRLEN * 3; i >= index; i--){
if(i != 0 && (str[i] != '\0' || i == strlen(str))){
str[i] = str[i - 1];
}
else if(i == 0){
str[0] = ' ';
}
}
str[index] = Char1;
}

void adday(char str[]) {
char consonants[42] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'};
for(int i = 0; i < STRLEN * 3; i++){
if(i==0){
for(int c = 0; c < 42; c++){
if(str[i] == consonants[c]){
insertaChar(str, 'y', i);
insertaChar(str, 'a', i);
break;
}
}
}
else if(i != 0){
if(str[i-1] == ' '){
for(int c = 0; c < 42; c++){
if(str[i] == consonants[c]){
insertaChar(str, 'y', i);
insertaChar(str, 'a', i);
break;
}
}
}
}
}
}

int findWords(char str[]){
int count = 0;
for(int i = 0; i < STRLEN * 3; i++){
char c = str[i];
if(isspace(c))
count++;
if(count == 2)
return i + 1;
}
return -1;
}

char* stringReorder(char str[], int i){
if(i == -1){
return str;
}
else{
char string1[STRLEN];
char string2[STRLEN * 2];
strncpy(string1, str, i);
strncpy(string2, &str[i], strlen(str) - i);
char string[STRLEN * 3];
strcpy(str, string2);
strcat(str, " ");
strcat(str, string1);
return str;
}
}

void main(void) {
char mystring[STRLEN * 3];
printf("** Welcome to the Double Dutch game **\n");
printf("Please enter a string: ");
scanf("%[^\n]s", mystring);
char vowel = 'e';
replaceVowelsA(mystring, vowel);
vowel = 'i';
replaceVowelsA(mystring, vowel);
vowel = 'u';
replaceVowelsA(mystring, vowel);
vowel = 'o';
replaceVowelsA(mystring, vowel);
adday(mystring);
int index = findWords(mystring);
strcpy(mystring, stringReorder(mystring, index));
printf("Double Dutch translation: %s", mystring);
}

Answer

In the function replaceVowelsA(), you replace the string you input with a specific character. In the function replaceVowelsA(), you use findOcurrace() to find if Char exist. if not it will return -1, but you didn't check the return value, so it may return -1, in the code line 22, it may execute code str[-1] = 'a', it's a very dangerous behavior.

There are also other problems with your string operation like terminate character NULL you didn't check.

you can add -g option if you use gnu toolchains. Also you can use other debug method to find the problem.

Comments