Hi i am in need of an explanation to the following:
I have created a program to take in a string(stored in a character array).
With my string,i must pass it into a function and 2 functions i have created are stringLength and toUpperCase.(i know there are string functions to do these,it is just practice.)
The following code is what i have done for this,however i am not sure that i am doing this correctly,as when i call the first function it will give me the length,but then crash.
Similarly,when i pass in the array to my second function,it will print out the string in upper case,but random characters will sometimes follow after.Even though i have used the '\0' test in the for loop to stop it once it reaches this character.
What i am basically asking is,am i passing it the array correctly with the use of pointers because i have just ended up confusing myself so much that i cant see a way around it.
Thanks for any help.
MAIN()
#include <iostream>
#include "string.cpp"
using namespace std;
main(){
const int max_chars =100;
int length= 0;
char* letters[max_chars + 1];
String* string;
string = new String();
cout << "Enter string: " ;
cin.getline(*letters, max_chars + 1, '\n');
string->stringLength(*letters);
string->toUpperCase(*letters);
}
Class implementation file
// Class implementation file
#include <iostream>
#include "String.h"
using namespace std;
String::String(){
length = 0;
letters[length];
}
// function to find length of string
void String::stringLength(char* _letters){
//letters[length];
//int length = 0;
for(length = 0; _letters[length] != '\0'; length++)
{
letters[length] = _letters[length];
}
cout << "Length of string is " << length << endl;
}
// Function to covert string to uppercase characters.
void String::toUpperCase(char* _letters){
int length;
// puts driver program array into function array
for(length = 0; _letters[length] != '\0'; length++)
{
letters[length] = _letters[length];
}
for(int i = 0; i < length; i++)
{
if( (letters[i]>=97) && (letters[i]<=122) ) // checking if character is lowercase.
{
letters[i] -= 32; // convert to uppercase.
}
}
cout << letters ;
}
#ifndef STRING_H
#define STRING_H
class String {
private:
int length;
char letters[];
public:
String();
void toUpperCase(char* _letters);
void stringLength(char* _letters);
};
#endif