/*i think its impossible to do without using arrays as string is an array of characters but it is possible to do without using pointers*/
void main()
{
char s[10],b[10];int x,k,i;
clrscr();
printf("enter a string\n");
gets(s);
x=strlen(s);
k=0;
for(i=x;i%26gt;0;i--)
{
b[k]=s[i-1];
k++;
b[k]='\0';
}
printf("the reversed string of %s is %s",s,b);
getch();
}
How can we get a string %26amp; reverse it ? without using pointers %26amp; arrays in C / C++?
Technically this is impossible because a string is a char array in c and an object representing a char array in c++. Therefore you MUST use arrays, and to access an array you need a pointer, consider:
char myArray[3]={0, 0, 0};
myArray[2]=3;
this is eqivelent to:
char myArray[3]=(0, 0, 0);
*(myArray+3)=0;
So to reverse a string use something like this:
string reverse(string str)
{
string result="";
for(int a=0; a%26lt;str.length(); a++)
{
result+=str[a];
}
return result;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment