This has been driving me nuts for the last half hour or so. Google didn't help. I don't know which function returns the nth character in the string. I know that it is supposed to be something like this

stringname.something(n?)

If someone could post the function and an example of how to use it, I would really appreciate it.

Recommended Answers

All 3 Replies

there is no string data type in c++... the string u enter is in char array.... so u already have the access to any index element in the c++ string

commented: Well, there is a string data type in the standard library. +0

If you are using char[] array or STL strings, just use the [] operator. e.g.

#include <string>
using namespace std;
...
string mystring = "hello";
v = mystring[n]; // v = nth character of string

// or
char mycharstring = "hello";
v = mycharstring[n]; // v = nth character of string

Or you could use std::string's at() method, but I don't see that very often. Read this about it.

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.