-1
char cstri[] = "hello world";

From here, is there any way to get a single char, such as the first e, position at 1, from this cstring?

I tried a few times, and every time it returns the entire string starting from the passed index. So, if I want 'e', position at 1, it returns ello world instead of just e.

I also tried to copy a single char from the string using strncpy() and memcpy(), but it copies the string from index 0 to null, or just the specified amount.

strncpy(b, cstri , 1);

I know a cstring is read-only, but is there no way to get a single char from a cstring?

I want to use printf(), so I can't use char b = cstri[1]

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Eric Lee
  • 1
  • 2
  • 1
    For starters, `char cstri = "hello world";` doesn't compile. *"want to use printf so I can't use char b = cstri[1]"* You absolutely can. There's `%c` for printing single characters. – HolyBlackCat Sep 26 '21 at 07:45
  • @HolyBlackCat sorry I forgot to [] after cstri. – Eric Lee Sep 26 '21 at 07:47
  • Why are you fiddling with all this C stuff if you want to program C++? Not that you cannot, you shouldn't. ;-) – Scheff's Cat Sep 26 '21 at 07:48
  • "*if I want 'e', position at 1, it returns ello world instead of just 'e'.*" - Then you are probably doing `cstri + 1` when you should be doing `cstr[1]` or `*(cstr + 1)`. You should consider showing the code, if not, at least a part of it. I say you at least get a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to understand how the language works. – Ruks Sep 26 '21 at 07:49
  • @Scheff'sCat Like few days ago, I misunderstood something so I thought about this then I just have some free time now and I decide to solve it. – Eric Lee Sep 26 '21 at 07:57
  • @Ruks I tried cstr[1] but its const char* so I can't use it for printf. And *(cstr +1) same reason. – Eric Lee Sep 26 '21 at 07:59
  • 1
    `printf("%c", cstr[1]);` or `printf("%c", *(cstr + 1));`? Better yet, `std::cout << cstr[1];` – Ruks Sep 26 '21 at 08:00
  • For `char cstri[] = "hello world;`, `cstri[1]` _cannot_ be `const char*`. It's `char` (or `char&`). If you really intend to use `printf()`, you have to provide the appropriate formatter: `printf("2nd char: %c\n", cstri[1]);`. – Scheff's Cat Sep 26 '21 at 08:01
  • Usage of `printf()` and correct formatters with correct arguments is not that easy and error-prone. Hence, the whole mess (adopted from C) was replaced by a type-safe C++ alternative: `std::cout << "2nd char: " << cstri[1] << std::endl;`. Hence, I wondered that you punish yourself with this C stuff... Nevertheless, it's never wrong to find something out... ;-) – Scheff's Cat Sep 26 '21 at 08:03

1 Answers1

0

Your question already hints on "C++" and given the additional fact that there is no difference in the memory representation of a C++ std::string and a normal c-string, the answer is simple: just learn how std::string works. Btw. std::string::c_str() then return your nice c-string.

#include <string>
#include <iostream>
int main() {
  std::string hw = "hello world";
  std::cout << hw << std::endl; // output: "hello world"
  std::cout << hw.c_str() << std::endl; // output: "hello world"
  std::cout << hw[1] << std::endl; // output: 'e'
  return 0;
}

But there are a ton of more features of std::string and also look at what #include <iomanip> can do.

Ralf Ulrich
  • 1,575
  • 9
  • 25