Solved

changing the size of a string

Posted on 2004-10-23
173 Views
Last Modified: 2010-04-01
The following code tries to remove the character 's' from the string. But the code doesn't work, i know its becoz of the if condition. Please help me in creating function that actually modifies the original string.
_________________________________________________________________________
      char s[] ="hissalpha";

      cout << s << endl;


      char *p=s;
      char *q=s;


      while (*p)
      {
      
            if (*p !='s')
      
            {
            
                  cout << *p << endl;

                  *q=*p;
                  
                   q++;
            }

      
            p++;
      }

      cout << s << endl;
0
Question by:anshuma
    6 Comments
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    It works well, just need an ending null character after the last line:

    *q = 0;
    cout << s << endl;
    0
     
    LVL 55

    Accepted Solution

    by:
    Sorry, I mean "need an ending null character **before** the last line"

    string size is determined by the position of the null character.
    0
     

    Author Comment

    by:anshuma
    i don't get it
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    >i don't get it
    That means you haven't understood the problem?
    I your code working well now?
    0
     

    Author Comment

    by:anshuma
    tell me one thing, what happens to the memory that was allocated for the bigger string. Like if it was size 10 in the beginning and then on removal of 3 chars it becomes size 7. I am sure we don't need delete because we are not using "new".
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    >I am sure we don't need delete because we are not using "new"
    That's correct.

    >what happens to the memory that was allocated for the bigger string
    As a general rule, every chunk of allocated memory in C/C++ (by using new or not) is deallocated entirely, so if you put an ending null character this will not alter the fact that 10 bytes will be deallocated, not 7. That's because deallocating routine doesn't inspect if a null character is present in buffer.

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now