Hello.

What I have to do is:
Take the first half of the input, and output it backwards. Length of the input is 10 or less.

Examples:
123456789 - entered by user;
54321 - result.
abcde - entered by user;
cba - result.

The catch is, that I can only use 2 variables: s and cpt. The only function I can use from string.h is strlen().
I know that there is function strrev() which would do the work, but Im not allowed to use it.

I'm being taught pointers and references now. I know how the & and * work. But I dont know how use them in this task.
If the input was 10 symbols long, I could just output s[4],s[3],s[2],s[1],s[0].

Would be great if someone could point me in the right direction.
I think my English is worse than usually today. Sorry for that.
Thanks.

#include <conio.h>
#include <string.h>
#include <iostream.h>

void main(){
char s[10];
char * cpt;
cpt=s;

// user is not allowed to create arrays with more than 10 symbols
do{
cout<<"Enter the text (number of symbols <=10 )\n";
cin>>s;
cout<<"Length of array: "<<strlen(s)<<"\n"<<"Entered array: "<<s<<"\n";
}while(strlen(s)>10);
cout<<"Middle element of arrray: "<<(strlen(s)+1)/2<<"\n";
cpt=&s[0];
// getting value
cout<<*cpt;
// getting reference
cout<<&cpt;

getch();
}

Recommended Answers

All 5 Replies

Try to understand this piece of code

#include <iostream>

using namespace std;

int main()
{
	char s[10];
	char *cpt;

	cout<<"Text: "; cin >> s;
	cout<<"Middle Array Element: " << strlen(s)/2 - 1;

	cpt = &s[0];

	cout << endl;

	for(int x=strlen(s)/2-1; x>=0; x--)
	{
		cout << cpt[x];
	}

	return 0;
}

Hope it helps

>// user is not allowed to create arrays with more than 10 symbols
Okay.

>cin>>s;
Oops, too late. There's nothing stopping the user from typing more than ten characters. You can check for a string length of ten or less, but by then any damage has already been done. You can fix it by only storing ten characters, regardless of how much the user types:

cin>> setw(10) >> s;

>Would be great if someone could point me in the right direction.
Assign cpt to s, then add half the length of the string to cpt. Now you can walk backward until cpt is equal to s.

I have the script working at the moment, but I'm using an extra int variable in the for loop. I didn't know about setw() before, thanks for letting me know.

Assign cpt to s, then add half the length of the string to cpt. Now you can walk backward until cpt is equal to s.

I believe it is a for loop you are suggesting, and it only uses cpt and s inside the brackets.

The problem I have now is that I am not able to pass the strlen(s) to an int type variable and use it in the for loop... because I don't have an int type variable. I have 2 variables, and they are both of char type.

I am not sure how you call the x in the foor loop. As far as I understand, it's type can only be int, and I can't think of a way of how to get an int type variable.
for(x=... ; x>=... ; x--){...};

Adding half of the length of the string to cpt

The error I get is:
Cannot convert unsigned int to * char.

>I believe it is a for loop you are suggesting, and it only uses cpt and s inside the brackets.
A for loop would work, yes. I was specifically thinking of a while loop though, and no extra variables beyond s and cpt would be needed.

Here's an example of how to iterate over a C-style string using a pointer. It should help you figure things out:

#include <iostream>

int main()
{
  const char *s = "1234567890";
  const char *p = s;

  while (*p != '\0') {
    std::cout<< *p <<'\n';
    ++p;
  }
}

Note that you can increment by more than one ++p increments by one, but p += 5 is just as valid (assuming the string is long enough).

Thanks.
Took me a bit to figure it out. I had similar exercises during the lecture today, and they seemed easy, haha.

Now, could someone suggest me a challenging and/or interesting pointer related task, so that I could think, practice and understand better? The tasks in lecture were one-sided. I like getting stuck somewhere and then having the sense of small accomplishment when I understand a new thing, even if it is very tiny.

#include <conio.h>
#include <string.h>
#include <iostream.h>

void main(){
	 char s[10]="abfcdeu";
	 char * cpt;
         cpt=s+(strlen(s)-1)/2;
         for(cpt ; cpt>=s ; cpt--)
         {
             cout<<*cpt;
         }

         getch();
}
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.