iT邦幫忙

0

函數回傳時一直出現存取違規

#include <stdio.h>
#include<stdlib.h>
#include <time.h>
#include<string.h>
#include<ctype.h>
void pigLatin(const char*);
char dMother(const char*);
int main(void) {
	srand(time(NULL) % 32767);
	const char* word[4][5] = {
		"the","a","one","some","any","boy","girl","dog","town","car","drove","jumped","ran","walked","skipped","to","from","over","under","on"
	};
	char sentence[20][35] = { '\0' };
	for (size_t j = 0; j <= 19; j++) {
		for (size_t i = 0; i <= 5; i++) {
			strcat(sentence[j], word[i % 4][rand() % 5]);
			if (i != 5)
				strcat(sentence[j], " ");
			else
				strcat(sentence[j], ".");
		}
		sentence[j][0] = toupper(sentence[j][0]);
		printf("%s", sentence[j]);
	}
	printf("\n");
	getchar();
	char paragraph[2000] = {0};
	for (size_t i = 0; i <= 19; i++) {
		dMother(sentence[i]);\\問題
	}
	printf("\n");
	getchar();
	
	for (size_t i = 0; i <= 19; i++) {
		pigLatin(paragraph[i]);
	}
	printf("\n");
	getchar();
}
char dMother(const char* instring) {
	int word = 1;
	char ans[45] = {0};
	char outstring[45];
	strcpy(outstring, instring);
	outstring[0] = tolower(outstring[0]);
	outstring[strlen(outstring) - 1] = '\0';
	char* tokenPtr = strtok(outstring, " ");
	static int c = 0;
	int len=0;
	
	while (tokenPtr != NULL) {
		char ansWord[10] = {0};
		if (word<=6) {
			int temp=0;
			for (size_t i = 0; i <= strlen(tokenPtr) - 2; i++) {
				if (tokenPtr[i] == 'a' || tokenPtr[i] == 'e' || tokenPtr[i] == 'i' || tokenPtr[i] == 'o' || tokenPtr[i] == 'u') {
					temp = i;
					break;
				}
			}
			len = strlen(tokenPtr);
			for (size_t i = strlen(tokenPtr); i > temp; i--) {
				tokenPtr[i] = tokenPtr[i - 1];
			}
			for (int i = 0; i <= len; i++) {
				if (word == 1 && i == 0) {
					printf("%c", toupper(tokenPtr[i]));
					ansWord[i] = tokenPtr[i];
				}
				else
					printf("%c", tokenPtr[i]);
				ansWord[i] = tokenPtr[i];
			}
			strcat(ans, ansWord);
		}
		word++;
		if (word <= 6) {
			printf(" ");
			strcat(ans, " ");
		}
		tokenPtr = strtok(NULL, " ");
	}
	printf(". ");
	strcat(ans, ". ");
	return ans;
}
void pigLatin(const char* instring) {
	char outstring[45];
	strcpy(outstring, instring);
	outstring[0] = tolower(outstring[0]);
	outstring[strlen(outstring) - 1] = '\0';
	char* tokenPtr = strtok(outstring, " ");
	static int c = 0;
	while (tokenPtr != NULL) {
		if (strlen(tokenPtr) != 1) {
			char temp = tokenPtr[0];
			for (size_t i = 0; i <= strlen(tokenPtr) - 2; i++) {
				tokenPtr[i] = tokenPtr[i + 1];
			}
			tokenPtr[strlen(tokenPtr) - 1] = temp;
		}
		if (0 == c) {
			printf("%say", tokenPtr);
			c++;
		}
		else {
			printf(" %say", tokenPtr);
		}
		tokenPtr = strtok(NULL, " ");
	}
	printf(".");
}

上面標記問題處想寫strcat(paragraph,dMother(sentence[i]))但一直顯示存取違規
不知道能否有大神方忙解惑

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-05-29 12:23:49
mac$ clang acsvio.c
mac$ ./a.out
Some dog drove under the boy.The town jumped to one boy.The boy skipped under any girl.One car jumped to some town.The dog walked over the car.Some dog drove over any boy.A dog ran to one girl.A car skipped under any boy.Any car jumped to some boy.Some girl ran over the town.A car drove under a town.Any girl walked to some car.The girl jumped on some car.Some dog jumped to any girl.A car skipped from some dog.Any dog jumped from a car.Any car skipped over some boy.The dog ran under the dog.Any girl ran over the girl.One town skipped under a girl.

Soome doog droove uunder tthe booy. ans=soome doog droove uunder tthe booy. 
Tthe toown juumped tto oone booy. ans=tthe toown juumped tto oone booy. 
Tthe booy skiipped uunder aany giirl. ans=tthe booy skiipped uunder aany giirl. 
Oone caar juumped tto soome toown. ans=oone caar juumped tto soome toown. 
Tthe doog waalked oover tthe caar. ans=tthe doog waalked oover tthe caar. 
Soome doog droove oover aany booy. ans=soome doog droove oover aany booy. 
Aa doog raan tto oone giirl. ans=aa doog raan tto oone giirl. 
Aa caar skiipped uunder aany booy. ans=aa caar skiipped uunder aany booy. 
Aany caar juumped tto soome booy. ans=aany caar juumped tto soome booy. 
Soome giirl raan oover tthe toown. ans=soome giirl raan oover tthe toown. 
Aa caar droove uunder aa toown. ans=aa caar droove uunder aa toown. 
Aany giirl waalked tto soome caar. ans=aany giirl waalked tto soome caar. 
Tthe giirl juumped oon soome caar. ans=tthe giirl juumped oon soome caar. 
Soome doog juumped tto aany giirl. ans=soome doog juumped tto aany giirl. 
Aa caar skiipped froom soome doog. ans=aa caar skiipped froom soome doog. 
Aany doog juumped froom aa caar. ans=aany doog juumped froom aa caar. 
Aany caar skiipped oover soome booy. ans=aany caar skiipped oover soome booy. 
Tthe doog raan uunder tthe doog. ans=tthe doog raan uunder tthe doog. 
Aany giirl raan oover tthe giirl. ans=aany giirl raan oover tthe giirl. 
Oone toown skiipped uunder aa giirl. ans=oone toown skiipped uunder aa giirl. 


....................

mac$ 

新增的程式碼以add標示
修改的程式碼以chg標示

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

// void pigLatin(const char*);
// char dMother(const char*);
void dMother(const char*);	//chg
void pigLatin(char* instring);	//chg
int main(void) {
	srand(time(NULL) % 32767);
	const char* word[4][5] = {
		"the","a","one","some","any","boy","girl","dog","town","car","drove","jumped","ran","walked","skipped","to","from","over","under","on"
	};
	char sentence[20][35] = { '\0' };
	for (size_t j = 0; j <= 19; j++) {
		for (size_t i = 0; i <= 5; i++) {
			strcat(sentence[j], word[i % 4][rand() % 5]);
			if (i != 5)
				strcat(sentence[j], " ");
			else
				strcat(sentence[j], ".");
		}
		sentence[j][0] = toupper(sentence[j][0]);
		printf("%s", sentence[j]);
	}
	printf("\n");
	getchar();
	char paragraph[2000] = {0};
	for (size_t i = 0; i <= 19; i++) {
		dMother(sentence[i]);//問題
	}
	printf("\n");
	getchar();
	
	for (size_t i = 0; i <= 19; i++) {
		pigLatin(&paragraph[i]);
	}
	printf("\n");
	getchar();
}
// char dMother(const char* instring) {
void dMother(const char* instring) {		//chg
	int word = 1;
	char ans[45] = {0};
	char outstring[45];
	strcpy(outstring, instring);
	outstring[0] = tolower(outstring[0]);
	outstring[strlen(outstring) - 1] = '\0';
	char* tokenPtr = strtok(outstring, " ");
	static int c = 0;
	int len=0;
	
	while (tokenPtr != NULL) {
		char ansWord[10] = {0};
		if (word<=6) {
			int temp=0;
			for (size_t i = 0; i <= strlen(tokenPtr) - 2; i++) {
				if (tokenPtr[i] == 'a' || tokenPtr[i] == 'e' || tokenPtr[i] == 'i' || tokenPtr[i] == 'o' || tokenPtr[i] == 'u') {
					temp = i;
					break;
				}
			}
			len = strlen(tokenPtr);
			for (size_t i = strlen(tokenPtr); i > temp; i--) {
				tokenPtr[i] = tokenPtr[i - 1];
			}
			for (int i = 0; i <= len; i++) {
				if (word == 1 && i == 0) {
					printf("%c", toupper(tokenPtr[i]));
					ansWord[i] = tokenPtr[i];
				}
				else
					printf("%c", tokenPtr[i]);
				ansWord[i] = tokenPtr[i];
			}
			strcat(ans, ansWord);
		}
		word++;
		if (word <= 6) {
			printf(" ");
			strcat(ans, " ");
		}
		tokenPtr = strtok(NULL, " ");
	}
	printf(". ");
	strcat(ans, ". ");
	printf("ans=%s\n", ans);	//add
// 	return ans;
}
// void pigLatin(const char* instring) {
void pigLatin(char* instring) {		//chg
	char outstring[45];
	strcpy(outstring, instring);
	outstring[0] = tolower(outstring[0]);
	outstring[strlen(outstring) - 1] = '\0';
	char* tokenPtr = strtok(outstring, " ");
	static int c = 0;
	while (tokenPtr != NULL) {
		if (strlen(tokenPtr) != 1) {
			char temp = tokenPtr[0];
			for (size_t i = 0; i <= strlen(tokenPtr) - 2; i++) {
				tokenPtr[i] = tokenPtr[i + 1];
			}
			tokenPtr[strlen(tokenPtr) - 1] = temp;
		}
		if (0 == c) {
			printf("%say", tokenPtr);
			c++;
		}
		else {
			printf(" %say", tokenPtr);
		}
		tokenPtr = strtok(NULL, " ");
	}
	printf(".");
}

不好意思應該是我沒說清楚
就是我這邊是希望把dMother的回傳值放進pigLatin涵式所以出現存取違規~

我要發表回答

立即登入回答