This is a very confused bit of code you have there...
You declare
a
as a
char
value:
char chng(char a){
Which means that you can only pass a single byte to the method.
But...you immediately start to use it as if it was an array of characters:
while(a[i]!='\0'){
That's bad, for a lot of reasons, all of them centered around the fundamental array definition is that
the name of an array is a pointer to the first element. Which mean that the system is trying to use your single byte value as a multibyte pointer - 4 bytes for a 32 bit system, 8 on a 64 bit). That isn't going to work, no matter what you do.
You are also trying to return a pointer as a single character, which isn;t goign to work either.
So start by changing your function declaration:
char* chng(char* input){
(Don't use single character names, it makes it a lot harder to read your code later - descriptive names for all your variables helps your code become self documenting).
Then change
main
to get a string instead of a character from your user, and pass that to the function. When you have that working, write another function which you pass a string and two integers, which reverses all the characters between the two indexes you pass. Use that to reverse the whole string for testing and print the result in your main method. When that all works, start working on leaving the special characters alone.
Doing it all in small stages like this means that each time you are only doing a little bit extra work, but that you know that everything it's based on is working. It may seem slower, but it gets to the solution a lot, lot faster than trying to write the whole thing at once and finding that something doesn't work!
Reply:
#include <iostream>
using namespace std;
char* chng(char* a){
int left,right,i=0,count=0;
char *temp,output;
left=0;
while(a[i]!='\0'){
count++;
i++;
}
right=count-1;
while(left<right){ if(!(a[left]="">='a'&& a[left]<='z')){
left++;
}
else if(!(a[right]>='a'&& a[right]<='z')){
right--;
}
else{
*temp=a[left];
a[left]=a[right];
a[right]=*temp;
}
left++;
right--;
}
output= *a ;
return output;
}
int main()
{
char* b;
cin>>b;
cout<<chng(b);
return 0;
}
Where do I start ...
Let's start at the beginning, because until you get the basics right, nothing else can be expected to work.
int main()
{
char* b;
cin>>b;
cout<<chng(b);
return 0;
}
Declaring a variable as
char*
doesn;t allocate any memory for your user to type into: so where is the data you read with
cin
supposed to go?
Remove the call to
chng
for a moment, and just get main working first - use
cout
to echo the user input back to him, and get that working.
I'm pretty sure you know how, or have been told how: this isn't "hello world" so user input is something you will have done before. Stop guessing what you need to do, and think about it - this isn't complicated!