Improve company productivity with a Business Account.Sign Up

x
?
Solved

changing the size of a string

Posted on 2004-10-23
6
Medium Priority
?
226 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
Comment
Question by:anshuma
  • 4
  • 2
6 Comments
 
LVL 55

Expert Comment

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

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

Accepted Solution

by:
Jaime Olivares earned 1000 total points
ID: 12389359
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
ID: 12389476
i don't get it
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 55

Expert Comment

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

Author Comment

by:anshuma
ID: 12389547
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
ID: 12389636
>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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

608 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