0

Please help to understand what's wrong with my simple C++ code

  #include <iostream>
  #include <cstring>
  #include <string>

  using namespace std;

  int main()
  {
      string s = "qwertyuiopasdfghjklzxcvbnm";
      string match = "w";
   
      int n = s.length();
   
      char char_array[n + 1];
   
      strcpy(char_array, s.c_str());

   
      for (int i = 0; i < n; i++)
          if (match.compare(char_array[i]) == 0) {
              cout << char_array[i]; 
          }
      return 0;
  }

I receive an error :

error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

Please help with this conversion char to *char and compare them correctly

1
  • 1
    You’re comparing a string (match) with a character. That is not supported. Compare to w[0] instead. But I’m also very puzzled by why you would copy the string to a char array to begin with. for (auto c : s) would loop over the characters just fine (not to mention that this looks like something where the find function might be more appropriate). Commented Oct 17, 2021 at 10:25

2 Answers 2

2

There are 2 mistakes in your given example.

Mistake 1

You have written the statement:

 char char_array[n + 1]; //since n is not a compile time constant so this is not standard C++

In C++, the size of an array must be a compile time constant. So you cannot write code like:

int n = 10;
int arr[n];    //incorrect

Correct way to write this would be:

const int n = 10;
int arr[n];    //correct

Mistake 2

You are trying to convert char to const char* as the error says in the compare() method.

If you just want to find out whether a given character occurs in a std::string then there are 2 options/solutions(maybe more).

Solution 1

 #include <iostream>
  #include <string>

  using namespace std;

  int main()
  {
      string s = "qwertyuiopasdfghjklzxcvbnm";
      char match = 'w';  //use char instead of std::string
   
      for (int i = 0; i < s.length(); i++)
          if (s[i] == match) {
              cout << "element: "<< match <<" found at index: "<<i<<std::endl; 
          }
      return 0;
  }

As shown in the above solution 1 you don't need to create a separate array.

Solution 2

You can use std::string::find to find a given substring inside another string. This would look like:

 #include <iostream>
  #include <string>

  using namespace std;

  int main()
  {
      string s = "qertyuiowpasdfghjklzxcvbnm";
      std::string match = "w"; //using std::string instead of char
   
      std::size_t index = s.find(match);
      
      if(index != std::string::npos)
      {
          std::cout<<"string: "<<match<<" found at index: "<<index<<std::endl;
      }
      else 
      {
          std::cout<<"string: "<<match<<" not found"<<std::endl;
      }
      return 0;
  }

The output of solution 2 can be seen here.

Solution 3


#include <iostream>
#include <string>
int main()
{
   std::string s = "qertyuiopasdfwghjklzxcvbnm";
   std::size_t index1 = s.find_first_of('w');

   if(index1 != std::string::npos)
   {
        std::cout<<"found at index: "<<index1<<std::endl;
  }
    else 
    {
        std::cout<<"not found"<<std::endl;
        
    }
    return 0;
}

Sign up to request clarification or add additional context in comments.

Comments

0

As it states in the error, you cannot convert a single character to a string. But you can use the std::string(size_t , char ) constructor.

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string s = "qwertyuiopasdfghjklzxcvbnm";
    string match = "w";
 
    int n = s.length();
 
    char char_array[n + 1];
 
    strcpy(char_array, s.c_str());

 
    for (int i = 0; i < n; i++)
        if (match.compare(string(1, char_array[i])) == 0) {
            cout << char_array[i]; 
        }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.