Printing one character in a string

Hello there! I encountered this problem during a technical interview.

In the question, a string array has the following elements: {"polymorphism", "inheritance", "classes"}
then using a pointer and some manipulations, the program was able to output "cancers".
wherein the letter "c" was extracted from "classes",
the letter "s" was from "polymorphism"
and the "ance" was from the last 4 letters of "inheritance".

I'm trying to retype the program again so I could understand the problem, but I forgot the program already.
You lost letter 'r'!:)
And I have not understood what problem are you trying to understand?
Last edited on
oh yeah i forgot r. the "r" is also from "polymorphism"

I forgot how the program went already and I'm trying to retype it but i couldn't because i don't get how the question was able to output only one character from a string. like for example the character "c" from "classes". And only using a pointer. This is what I got so far:

#include <iostream>
using namespace std;
int main(){
string x[] = {"polymorphism", "inheritance", "classes"};
string *ptr = x;
ptr = &x[2];
cout<<*ptr;
}

the output is the word "classes". Now how do I manipulate it to output only the letter "c"?
Last edited on
nope, the program didn't use substring. and the strlen cannot be used for string x because x is an array of strings?
strlen() cannot be used because it expects a const char* as argument, not an std::string nor an array of those. I don't see how it would be useful anyway.

Are you sure they were strings and not c-strings?
Last edited on
so how to do it without using whatever those links are up there. or if that's the way to do it, how can it be done?
This doesn't use pointers but it gets the job done.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream.h>
#include<cstring.h>
#include<conio.h>

main()
{
 string x[]={"polymorphism","inheritance","classes"};
 string word;
 int    y;

 //"c"
 word=x[2];
 cout<<word[0];

 //"ance"
 word=x[1];
 for(y=7;y<=10;y++)
  cout<<word[y];

 //"r" and "s"
 word=x[0];
 cout<<word[6];
 cout<<word[10];
 
 getch();
 return 0;
}
Spot3 wrote:
This doesn't use pointers
Lines 7, 12, 13, 16, 18, 21, 22, and 23 all use pointers.

Also, your #include statements use the old deprecated headers with .h, which is bad.
Sorry, i meant they didn't use non-array pointers. Technically it does because arrays are pointers. And what is wrong with the .h headers?
<iostream.h> is deprecated and most compilers don't support it anymore.
<iostream> is used instead.
http://www.cplusplus.com/reference/iostream/

<ctstring.h> is deprecated and most compilers don't support it anymore.
<ctstring> is used instead.
http://www.cplusplus.com/reference/cstring/

<conio.h> is non-standard and most compilers don't support it.
http://en.wikipedia.org/wiki/Conio.h
Last edited on
@Spot3
And what is wrong with the .h headers?


For example header string.h is a header that contains declarations of standard C functions. It does not contain the definition of class std::basic_string according to the C++ standard.
Last edited on
Thanks spot3! it's not the same as the program I saw but at least now I know how to extract one character or multiple characters from a string
Topic archived. No new replies allowed.