For ex:-if I enter string as John Smith and n=0 then it should output string as John and if n=2 then ohn.
I tried to code this program in C/C++ but vain.Segmentation faults is the only result that I am getting.
Here is the code that I am working on.
Expand|Select|Wrap|Line Numbers
- #include <stdlib.h>
- #include<string>
- #include<iostream>
- #include<ctype.h>
- using namespace std;
- int main()
- {
- char a[20];
- int i,n;
- cout<<"Enter the desired string";
- cin.getline(a,20);
- cout<<"Enter string number\n";
- cin>>n;
- if(n==0)
- {
- cout.write(a,20);
- }
- else
- {
- cout<<"The required string is \n";
- i=n;
- while(a[i]!=' '||a[i]!='\0')
- {
- cout<<a[i];
- i++;
- }
- }
- return 0;
- }
I am also confused between cin and cin.getline functions.Suggestions,hints are welcome.
Thanks in advance.
Cheers.