| | |
[C++] Problem with changing contents of char[]
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 57
Reputation:
Solved Threads: 0
Hi,
I'm quite new to C++ and I'm trying to write a function which would change uppercase letter in char[] to lowercase. Inside this function everything is fine but when I leave it, contents of char[] is not changed. Any ideas why? Also when I try to delete local pointers I'm getting error. Thanks for any help with this.
I'm quite new to C++ and I'm trying to write a function which would change uppercase letter in char[] to lowercase. Inside this function everything is fine but when I leave it, contents of char[] is not changed. Any ideas why? Also when I try to delete local pointers I'm getting error. Thanks for any help with this.
c++ Syntax (Toggle Plain Text)
void to_lower(const char* s) { if (!s) return; //if s doesn't point to anything int size = 5; char* new_cstring = new char[size]; for (int i = 0; i < size; new_cstring[i] = 0, ++i); //initialize array with 0 int counter = 0; //checks if there are still characters in the array while (*s) { //if it's an UPPERCASE letter if((int)*s >= 65 && (int)*s <= 90) { new_cstring[counter] = ((int)*s + 32); //changes this letter to lowercase } else { new_cstring[counter] = *s; } ++counter; ++s; if (counter == size - 1) { //creating new array to store contents of new_cstring char* arr = new char[counter]; for (int i = 0; i < counter; arr[i] = 0, ++i); //initializes arr with 0 int i = counter; for (; i >= 0; arr[i] = new_cstring[i], --i); //copies from old array to new one new_cstring = new char[counter + 10]; //creates new bigger array for(int i = 0; i < (counter + 10); new_cstring[i] = 0, ++i); //initialize new_string with 0 for (int i = 0; i < counter; new_cstring[i] = arr[i], ++i);//copies from arr to new_cstring size = counter + 10; //delete[] arr; } } s = new_cstring; }
you need to pass the reference to the pointer to make it work. You are just modifying the copy of the pointer here. So the changes will not be visible once you leave the fn.
fn signature could be
while calling you could do
fn signature could be
c++ Syntax (Toggle Plain Text)
void to_lower(const char*& s)
while calling you could do
c++ Syntax (Toggle Plain Text)
char s[] = "Test"; const char* p = s; to_lower(p); cout << p << endl;
thanks
-chandra
-chandra
Or those of you that like using std:strings etc.
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <cctype> #include <iostream> #include <string> int lowercase ( int c ) { return std::tolower ( (unsigned char)c ); } int main() { std::string foo ( "THIS IS A TEST" ); std::cout<< foo <<'\n'; std::transform ( foo.begin(), foo.end(), foo.begin(), lowercase ); std::cout<< foo <<'\n'; std::cin.get(); }
*Voted best profile in the world*
![]() |
Similar Threads
- unexpected behavior ifstream.open() (C++)
- Problem changing contents of JTable when button is pressed... (Java)
- problem in padding a string (C++)
- Storing Hex values in dynamically allocated char array (C)
- Problem With char (C++)
- SATA HDD - problem after changing computer (Storage)
Other Threads in the C++ Forum
- Previous Thread: Copying a microsoft word doc
- Next Thread: Arrays in Visual C++
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class code coding compile console conversion count data database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






