[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: atch is an unknown quantity at this point 
Solved Threads: 0
atch atch is offline Offline
Junior Poster in Training

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

 
0
  #1
Jul 13th, 2009
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,363
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

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

 
0
  #2
Jul 13th, 2009
>>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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 442
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

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

 
0
  #3
Jul 13th, 2009
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

  1. void to_lower(const char*& s)

while calling you could do

  1. char s[] = "Test";
  2. const char* p = s;
  3. to_lower(p);
  4. cout << p << endl;
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
1
  #4
Jul 13th, 2009
Or those of you that like using std:strings etc.

  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. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 57
Reputation: atch is an unknown quantity at this point 
Solved Threads: 0
atch atch is offline Offline
Junior Poster in Training

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

 
0
  #5
Jul 13th, 2009
Ok, thanks I'll try it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC