Remove a character from a string C++?
-
My array contains "b12". How can i remove the "b" and be left with the "12"
-
Answer:
can you paste the full code so I can edit
Bilal at Yahoo! Answers Visit the source
Other answers
I assume you mean C strings (the C++ language has all of the C data types) in char[] arrays, as in: char mystring[20] = "b12"; // a string with room for 19 chars, initially "b12" To delete the 'b', what you actually do is move the rest of string "12" left one position. The easiest way to do that is to use memmove from the <cstring> library. (not <string>) memmove(mystring, mystring+1, strlen(mystring+1)+1); The strlen function returns the length of the string whose start is given by the pointer argument. strlen(mystring+1) gives the length of the "12" portion of the "b12" string. i.e. 2 The memmove function just moves bytes, and the arguments are: mystring -- the destination. The moved string lands on the 'b' in "b12" mystring+1 -- the source. The chars to move start with the '1' in "b12" strlen(mystring+1)+1 -- the length. This computes the size of the "12" string, then adds one to also move the trailing 0 byte. There's another function, memcpy(), that uses the same arguments, and also the more usual strcpy() function that is easier to use, but these should NOT be used within the same string. The results of those is undefined when the source and destination overlap in memory. The memmove() function will properly handle overlaps, at the expense of a little extra code to check for that and choose the proper loop. Take a look at the earlier reply by odimwitdwon. He shows how to write your own loop. That's important to know. Also, if this is for a lab in a beginning C class, that's probably what you're expected to do.
??? Your question is far too vague. char s[3] = 'b12' ??? I assume that you know that '12' is still a string. say char s[50] ='We hold these truths to b12' and you want it to be 'We hold these truths to 12' You could use either a for loop or a while loop It depends on whether the string is class string object or a char[] array (whether the string is null terminated). In the simple case of an array, you want to read each char until you find the first 'b' and then remove it. Assuming you want to shorten the string by 1 this entails the following create a temporary string lets call the two strings s1 and s2 while s1[i] !='b' s2[i] = s1[i] ← this is pseudo_code and your actual while loop will be slightly different while <<not end>> s2[i] = s1[i+1] if the string can contain more than 1 'b' then you would use two indexes i & j and structure a single loop to keep track of both i and j with a if-else clause, then assigning s2[j] = s1[i]. i = 0, j=0 While <<not at end of s1>> if s1[i] !='b' { s2[j] = s1[i]; i++; j++;} else { ++i; s2[j] = s1[i]; i++; j++;} // skip an i
Related Q & A:
- How to split a string in C++?Best solution by Stack Overflow
- How can I remove a contact from a list in my address book?Best solution by windows.microsoft.com
- How do you remove a stereo from a 1996 Mercedes Benz c220?Best solution by Yahoo! Answers
- How to remove a bed off a ford ranger?Best solution by wiki.answers.com
- How do i isolate a in p=a+b+c?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.