Issues with potential stack overflow.

I'm working on a homework assignment that I've worked out successfully in each function except the last. My last function breaks/overflows the output of all my previous code. I did some research, and I'm thinking it is an overflow issue with my pointers, but I'm honestly stuck and unsure. I apologize if I've posted too much code, I cut out what I thought would be relevant to understanding the issue. Thank you for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  main() {
	// Declarations for string arrays and size varible.
	const int SIZE = 81;  // Sentence character cap is 80, the +1 holds the null terminator
	char sentence[SIZE];
	char ecnetnes[SIZE]; // muahahahaha
	// Populating sentence array
	cout << "Input a sentence that is no longer than 80 characters, spaces included as characters." << endl;
	cin.getline(sentence, SIZE);
	// Calling the find length function to determine true length of string array
	int length = findLength(sentence);
	// Calling conver caps function to capitalize the entire string array
	// Declaring pointer to pass the captilization changes made during function call
	char *sentencePTR = sentence;
	convertCaps(sentencePTR, length);
	// Calling count letter function to count the number of letter within the string array
	int letters = countLetters(sentence);
	//Calling count vowels function to count and return the number of vowels within in the string array
	int vowels = countVowels(sentence);
	// Calling function to count and return the number of words within the string array
	int words = countWords(sentence);
	// Calling count conso function to count and return the number of consonants within the string array
	int conso = countConso(sentence);
	// Calling reverse string function to fill a 2nd string with the contents of the first string, then reverse the contents
	// Creating another pointer for editing the second char array.
	char *ecnetnesPTR = ecnetnes;
	reverseString(ecnetnesPTR, length, sentence);


// function that is causing issues
void reverseString(char* ptr, int size, char str[]) {
	int index = size-1;
	int i = 0;
	while (ptr[i] != '0') {
		ptr[i] = str[index];
		index--;
		i++;
	}
}



Where do you put the \0 on the end of your reversed string?
I swapped the '\0' for size-1 instead and now we're rockin and rollin. Thanks man!
Last edited on
Topic archived. No new replies allowed.