I want to write a code that sort a cstring and put all the correct character into a new cstring,
Here is my Code.

#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
  char array[200]={"How Are U?"};
  char array1[200];
  int n=0;
  for(int i=0;array[i]!='\0';i++)
{
   if(islower(array[i])||array[i]==' ')
     {
     strcpy(array1[n],array[i]);
     n++;
}
}
}

but I get this when I try to compile
“invalid conversion from 'char' to 'char*'”
What should i do to make this right? Thanks!

Dani AI

Generated

Quick diagnosis and types
The error comes from passing single char values where a char* (pointer to a C‑string) is required. In the small example posted: myString decays to char*, myString[2] is a char, and &myString[2] is a char*. That explains the "invalid conversion from 'char' to 'char*'". is right that functions like strcpy expect NUL‑terminated strings, and was pointing toward copying characters one by one — but there are a couple of important safety details to add.

Recommended approach (modern and safe)
Prefer std::string to build the filtered result, then convert to a C‑string only if you really need it. Use std::copy_if or push_back to collect characters and always cast to unsigned char before calling std::islower to avoid undefined behavior on negative char values. Example pattern:

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>

const char src[] = "How Are U?";
// collect allowed chars into a std::string
std::string filtered;
std::copy_if(src, src + std::strlen(src), std::back_inserter(filtered),
             [](unsigned char c){ return std::islower(c) || c == ' '; });
std::cout << filtered << '\n'; // filtered.c_str() gives a null-terminated C-string

Practical cautions
If you must use raw char[], track the write index, check bounds, and explicitly append '\0' after the loop. If you intended to alphabetically sort the kept characters (not just filter them), call std::sort on the std::string (or vector<char>) after collecting characters. See cppreference for details on std::islower (cast to unsigned char) and std::string::c_str() for safe C‑string access: islower, std::string::c_str, .

Recommended Answers

All 5 Replies

What are the types of one, two and three in this example?

char myString[20];

??? one   = myString;
??? two   = myString[2];
??? three = &myString[2];

strcpy takes a char* as first and second argument.

This function strcpy() expects c-strings not characters

strcpy(array1[n],array);

array1[n] and array are characters.

>>strcpy(array1[n],array);

Just use simple assignment array1[n] = array[i];

Why is it when I try to get an OP to think about the problem himself that other people just tend to give away the answer - he's just going to do what you say now without actually understanding what's happening.

>>strcpy(array1[n],array);

Just use simple assignment array1[n] = array[i];

But array1 would not be a cstring, I want to copy the desired element from array without changing anything of array1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.