-2

I am suppose to go trough a string and remove the letter g,with out using any built in functions, only one variable,that has to be a pointer and no brackets allowed. I have the code,but it keeps returning an empty string and not the new edited string.

#include <iostream>

using namespace std;


void deleteG(char *str) {
    char *temp = str; //make new pointer,pointing at existing, now i have initialized and enough size.

    while (*str != '\0') { //while the c-string does not reach null termination

        if (*str != 'g' || *str != 'G') { // if the value of the current position is not the character g or G proceed.
            *temp = *str;//copy value over
            temp++;//increase count
        }
        str++;//increase count to next char and check again above is if does not equal g or G
    }
//this should now copy the new string over to the old string overriding all characters 
    while (*temp != '\0') {
        *str = *temp;
        str++;
        temp++;

    }

}

int main() {
    char msg[100] = "I recall the glass gate next to Gus in Lagos, near the gold bridge.";
    deleteG(msg);
    cout << msg;  // prints   I recall the lass ate next to us in Laos, near the old bride.
}
Charles
  • 1,384
  • 11
  • 18
  • 6
    Everything is either not `g` or not `G`. – David Schwartz Oct 04 '16 at 00:39
  • yes,copy string with out g or G – Robert Hernandez Oct 04 '16 at 00:47
  • 2
    You gotta use && instead of ||, otherwise anything will pass – ZenJ Oct 04 '16 at 00:48
  • 4
    That makes no sense. *Everything* is not `g` or not `G`. The only thing for which "not `g` or not `G`" is false would be something that is both `g` and `G`, which nothing is. Imagine every letter from A to Z and a to z. You put all the ones that are not `G` or not `g` in a pile -- every letter will be in that pile. What letter would not be in that pile? – David Schwartz Oct 04 '16 at 00:49
  • @DavidSchwartz ahhhhh i see, correct. makes sense thank you, that pretty much helped me out get it going. Just trying to figure out how to add the null terminating character,might have to do a different type of loop. thanks – Robert Hernandez Oct 04 '16 at 01:01
  • By the way, you can do do this with a single loop. It's simpler than the code you have here. – Mark Ransom Oct 04 '16 at 01:47
  • Seeing as you all ready have answers about your problem, some simple code for your consideration `void deleteG(char *src) { char *dest = src; do { while (*src == 'G' || *src == 'g') src++; } while (*dest++ = *src++);` – chux - Reinstate Monica Oct 04 '16 at 02:04

2 Answers2

3
if (*str != 'g' || *str != 'G') {

This condition is always true so it always copies the character.

Why is it always true, you ask?

Think about it - the character is either g, or G, or something else.

If it's g, then *str != 'g' is false, and *str != 'G' is true, and false || true is true, so the condition is true.

If it's G, then *str != 'g' is true, and *str != 'G' is false, and true || false is true, so the condition is true.

If it's something else, then *str != 'g' is true, and *str != 'G' is true, and true || true is true, so the condition is true.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

Change it to:

if (*str != 'g' && *str != 'G') {

This condition checks that the letter is not g, irrespective of case.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • @BaummitAugen I did? BTW, I deleted my comment regardless as the post says `with out using any built in functions`. – Ken Y-N Oct 04 '16 at 00:43
  • 1
    @KenY-N Yep, because https://stackoverflow.com/questions/21805674/do-i-need-to-cast-to-unsigned-char-before-calling-toupper As I said, C++ is terrible sometimes. (For reference, Ken suggested to compare `std::toupper(*str) != 'G'` instead of having to compares with an `&&`. I claimed that's potentially UB.) – Baum mit Augen Oct 04 '16 at 00:57
  • also had to edit code to move null character forward when reaching end of old string,and making he new shorter so nothing else would print. – Robert Hernandez Oct 04 '16 at 01:37