943,731 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 554
  • C++ RSS
Jul 13th, 2009
0

[C++] Problem with changing contents of char[]

Expand Post »
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.
c++ Syntax (Toggle Plain Text)
  1. void to_lower(const char* s)
  2. {
  3. if (!s) return; //if s doesn't point to anything
  4. int size = 5;
  5. char* new_cstring = new char[size];
  6. for (int i = 0; i < size; new_cstring[i] = 0, ++i); //initialize array with 0
  7. int counter = 0;
  8. //checks if there are still characters in the array
  9. while (*s)
  10. {
  11. //if it's an UPPERCASE letter
  12. if((int)*s >= 65 && (int)*s <= 90)
  13. {
  14. new_cstring[counter] = ((int)*s + 32); //changes this letter to lowercase
  15. }
  16. else
  17. {
  18. new_cstring[counter] = *s;
  19. }
  20. ++counter;
  21. ++s;
  22. if (counter == size - 1)
  23. {
  24. //creating new array to store contents of new_cstring
  25. char* arr = new char[counter];
  26. for (int i = 0; i < counter; arr[i] = 0, ++i); //initializes arr with 0
  27. int i = counter;
  28. for (; i >= 0; arr[i] = new_cstring[i], --i); //copies from old array to new one
  29. new_cstring = new char[counter + 10]; //creates new bigger array
  30. for(int i = 0; i < (counter + 10); new_cstring[i] = 0, ++i); //initialize new_string with 0
  31. for (int i = 0; i < counter; new_cstring[i] = arr[i], ++i);//copies from arr to new_cstring
  32. size = counter + 10;
  33. //delete[] arr;
  34. }
  35. }
  36. s = new_cstring;
  37.  
  38. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atch is offline Offline
58 posts
since Jul 2009
Jul 13th, 2009
0

Re: [C++] Problem with changing contents of char[]

>>s = new_cstring;

That is only changing the local copy of the string, not the original. Do not create a new character array, but work directly with the pointer that is in the parameter to that function.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 13th, 2009
0

Re: [C++] Problem with changing contents of char[]

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

c++ Syntax (Toggle Plain Text)
  1. void to_lower(const char*& s)

while calling you could do

c++ Syntax (Toggle Plain Text)
  1. char s[] = "Test";
  2. const char* p = s;
  3. to_lower(p);
  4. cout << p << endl;
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 13th, 2009
0

Re: [C++] Problem with changing contents of char[]

Or those of you that like using std:strings etc.

C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. int lowercase ( int c )
  7. {
  8. return std::tolower ( (unsigned char)c );
  9. }
  10.  
  11. int main()
  12. {
  13. std::string foo ( "THIS IS A TEST" );
  14.  
  15. std::cout<< foo <<'\n';
  16. std::transform ( foo.begin(), foo.end(), foo.begin(), lowercase );
  17. std::cout<< foo <<'\n';
  18.  
  19. std::cin.get();
  20. }
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 13th, 2009
0

Re: [C++] Problem with changing contents of char[]

Ok, thanks I'll try it.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atch is offline Offline
58 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Copying a microsoft word doc
Next Thread in C++ Forum Timeline: Arrays in Visual C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC