1

I am writing a program to remove the character "&" from a char array if it exist and display the output. Here is my code:

#include <iostream>
#include <string>
using namespace std;

void decrypt(char mess[1024])
{
    int i = 0;
    int decmp = 0;
    int len = strlen(mess);
    char decmess[256] = {0};
    while (i < len)
    {
        if (strcmp(&mess[i], "&") ==    0) {
            i++;
        } else {
            decmess[decmp] = mess[i];
            i++;
            decmp++;
        }
    }
    cout << decmess <<  endl;
}
int main(int argc, char *argv[])
{
    char encmess[1024] = {0};
    cout << "Enter message to decrypt" << endl;
    cin.getline(encmess, 1024);
    decrypt(encmess);
}

But it just isnt working only "&" from last of the string is removed if exist.

Sample input/output:

input: sg&shx&f
output: sg&sh&f
input: dh&st&
output: dh&st

Please help!!!

Croximo Crox
  • 79
  • 10

2 Answers2

3

You are overly complicating things mixing strlen() and char[] and #include <string>. You rely on initialization without ever affirmatively nul-terminating decmess, you don't need strcmp() to compare a single character.

It appears you wanted to do:

#define MAXC 1024           /* if you need a constant, #define one (or more) */

void decrypt(char *mess)
{
    char decmess[MAXC] = "";
    int i = 0, decmp = 0;
    
    for (; mess[i]; i++)
        if (mess[i] != '&')
            decmess[decmp++] = mess[i];
    
    decmess[decmp] = 0;     /* affirmatively nul-terminate despite initialization */
    
    std::cout << decmess << '\n';
}

(note: a C-string is nul-terminated there is no need to get the length before iterating over the characters it contains, just loop until you find the nul-character (ASCII 0). Also, main() taking no arguments is just int main() or int main(void))

As to why what you did didn't work, strcmp(&mess[i], "&") compares & with whatever is left in mess starting at mess + i so you never match '&' alone. &mess[i] is simply a pointer to the ith character in mess and using strcmp compares from that position to end of string with "&". (you can limit that with strncmp(&mess[i], "&", 1) -- but that is superfluous for if (mess[i] == '&')

Adding your short main() you would have:

#include <iostream>

#define MAXC 1024           /* if you need a constant, #define one (or more) */

void decrypt(char *mess)
{
    char decmess[MAXC] = "";
    int i = 0, decmp = 0;
    
    for (; mess[i]; i++)
        if (mess[i] != '&')
            decmess[decmp++] = mess[i];
    
    decmess[decmp] = 0;     /* affirmatively nul-terminate despite initialization */
    
    std::cout << decmess << '\n';
}

int main(void)
{
    char encmess[MAXC] = "";
    
    std::cout << "Enter message to decrypt" << '\n';
    std::cin.getline(encmess, MAXC);
    
    decrypt(encmess);
}

(note: see: Why is “using namespace std;” considered bad practice? and C++: “std::endl” vs “\n”)

Example Use/Output

$ ./bin/decmess
Enter message to decrypt
sg&shx&f
sgshxf

Let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Yeah, it works, and it fixed the error. I am new to programming so I write complicated stuff most of the time. – Croximo Crox Aug 27 '20 at 06:16
  • 1
    No problem, refinement comes with experience. Get it working first -- then work on refining `:)` – David C. Rankin Aug 27 '20 at 06:16
  • 1
    Sure `strcmp(&mess[i], "&"` compares `&` with whatever is left in `mess` starting at `mess + i` so you never match `'&'` (you could do `strncmp (&mess[i], "&", 1)` -- but why) – David C. Rankin Aug 27 '20 at 06:18
2

Because you tagged your question with c++ here is the c++-way of doing this:

#include <algorithm>
#include <string>

void decrypt(std::string& mess)
{
    mess.erase(std::remove_if(mess.begin(), mess.end(), [](auto c) {return c == '&'; }), mess.end());
}
Thrasher
  • 138
  • 10