[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

changing the size of a string

Posted on 2004-10-23
6
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.
Suggested Courses

649 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