| Back to Questions Page |
| |
| Question |
Can we have a private virtual method ? |
Rank |
Answer Posted By |
|
Question Submitted By :: Wilbur J. Pereira |
| This Interview Question Asked @ Ness-Technologies |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | No, we can't have private vitual method.
 |
| Rajendra Gandhi |
| |
| |
| Answer | no, we cann't hav private virtual method as virtual is used
in case of inheritance but private members cannot be
inherited..  |
| Nidhi Singh |
| |
| |
| Answer | Yes , It we can have a private virtual method. It has
nothing to do with inheritance. It only resolve the dynamic
binding . Only difference is that , this method should be
used inside the class defination of the Base .
The derived class may implements the method either
public,private or protected.
Exa:
class A
{
virtual void x(){cout<<"A"<<endl;};
public:
};
class B:public A
{
//virtual void x(){cout<<"B"<<endl;}; // no prob
public:
virtual void x(){cout<<"B"<<endl;};
};
int main()
{
A *a;
B *b=new B;
a=b;
a->fn();
}
Out put:
B  |
| Gopinath Das |
| |
| |
|
|
| |
| Answer | We can have virtual functions as long as we dont call the
function from a base pointer pointing to the base
class/derived class.
If we call then gives error.
The same error can be tested by compiling and running above
example.  |
| Nk |
| |
| |
| Answer | We can have the private virtual method. But it can be
accessed only through the derived class not through the
base class.
class A
{
private:
virtual void fun() { std::cout << "A::fun" <<
std::endl; }
};
class B : public A
{
public:
virtual void fun() { std::cout << "B::fun" <<
std::endl; }
};
int main(int argc, char* argv[])
{
A* pa = new A();
((B*)(pa))->fun();
}
Output : A::fun  |
| Sriram |
| |
| |
| Question |
Can we have a private constructor ? |
Rank |
Answer Posted By |
|
Question Submitted By :: Wilbur J. Pereira |
| This Interview Question Asked @ Ness-Technologies |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | yes
and if a constructor is made private/protected...then that
class cannot be inherited  |
| Prabha Govind |
| |
| |
| Answer | no,
we can not make a constructor private , if we make a
constructor private its method cannot access in main method .  |
| Ravi` |
| |
| |
| Answer | We can make a constructor Private or protected.
1. Private constructor is classic example of implementing a
singleton class( A class with a single instance)
2. Protected constructors can be used when the class cannot
be instantiated but can only be inherited.  |
| Srikanth |
| |
| |
| Answer | yes , we can creat private constructor trrough static
method we can access the class (constructor), for example
singleton, there are a lot of use in design pattern
here is example of single ton
class Singleton {
static Singleton s;
int i;
Singleton(int x) : i(x) { }
void operator=(Singleton&);
Singleton(const Singleton&);
public:
static Singleton& getHandle() {
return s;
}
int getValue() { return i; }
void setValue(int x) { i = x; }
};
Singleton Singleton::s(47);
int main() {
Singleton& s = Singleton::getHandle();
cout << s.getValue() << endl;
Singleton& s2 = Singleton::getHandle();
s2.setValue(9);
cout << s.getValue() << endl;
} ///:~  |
| Baikunta |
| |
| |
| Answer | 1. Yes we can make a constructor private. By implementing
this concept we can create a singleTon class.
2. Suppose we have a static method is a class that is used
to create the object of the class by using private
constructor then that member function is named as "Named
Constructor".
3. Using this named constructor concept we can create
SingleTon class as well as normal class.
Example:
class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};
Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}
Singleton *p1 = Singleton::Instance();
Singleton *p2 = p1->Instance();
Singleton & ref = * Singleton::Instance();  |
| Arun |
| |
| |
| Answer | #include<iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();
private:
static Singleton* pinstance;
Singleton();
};
Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}
void main()
{
Singleton *p1 = Singleton::Instance();
cout<<p1<<endl;
Singleton *p2 = p1->Instance();
cout<<p2<<endl;
Singleton & ref = * Singleton::Instance();
}  |
| Arun |
| |
| |
| Answer | #include<iostream>
using namespace std;
class A
{
int value;
A* ptr;
A()
{
cout<<"\n\t\tConctructor\n";
}
public:
static A* CreateObject()
{
A* ptr=NULL;
ptr=new A;
return ptr;
}
void getdata()
{
cout<<"\n\tEnter the Value of A class\t:\t";
cin>>value;
}
void putdata()
{
cout<<"\n\t\tThe Value of A Class\t:\t";
cout<<value<<endl;
}
~A()
{
cout<<"\n\t\tDestructor\n";
}
};
int main()
{
A *ptr,*ptr1,*ptr3;
ptr=A::CreateObject();
ptr1=A::CreateObject();
ptr3=A::CreateObject();
ptr->getdata();
ptr1->getdata();
ptr3->getdata();
ptr->putdata();
ptr1->putdata();
ptr3->putdata();
delete ptr;
delete ptr1;
delete ptr3;
return 0;
}
Ref:
Singleton and Factory
Classes...  |
| Manjunath |
| |
| |
| Answer | Used mainly to control the object creation...
Ex: the Number of user Login's we can restrict to 15 by
creating 15 objects...  |
| Manjunath |
| |
| |
| Question |
what is multi level inheritance give n example ? |
Rank |
Answer Posted By |
|
Question Submitted By :: Wilbur J. Pereira |
| This Interview Question Asked @ Ness-Technologies , Hulas Steel Company |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | The best example is Say we have 3 class
ClassA,ClassB and ClassC.
ClassB is derived from ClassA and ClassC is derived ClassB
this is multi level inheritance..
ClassA
^
|
|
ClassB
^
|
|
ClassC  |
| Roshan P R |
| |
| |
| Answer | derived a new class from another derived class.It is known
as multilevel inheritance.
example:
grandfather->father->son.  |
| Aswini.s |
| |
| |
| Question |
features of OOPS |
Rank |
Answer Posted By |
|
Question Submitted By :: Wilbur J. Pereira |
| This Interview Question Asked @ Ness-Technologies |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | 1. Encapsulation: Binding of Code and data together in a
single entity
2. Polymorphism: One name many properties (funtion
Overloading)
3. Inheritance:Acquiring properties of one object to another  |
| Praveen |
| |
| |
| Answer | class,objects,inheritance,encapsulation,constructor,abstrcti
on,polymorphism.  |
| Vandana |
| |
| |
| Answer | object oriented programmimg structures
 |
| Varsha Vilas Kalebag |
| |
| |
| Answer | Abstration : hiding essential features and showing non
essential features.
Encapsulation : grouping the code and data.
Inheritence : aquiring the properties of one class into other.
polymariphism : one function many forms.  |
| Ravi |
| |
| |
| Answer | Class: an Abstract Model of a real world entity or Concept
or thinking. Like Car , Human being, PayRoll,etc.
Object: An instance( which is besically representing the
class)shows class's attributes through some public or
private method(internally)
Method: private or public functions associted with a Calss
to access the Class (Object)
Massage Passing: Interaction(exchanging data) between two
objects
Abstraction : This is a concept by which we only acess the
necessary attributes of that calsss without knowing more
about the other attrkibutes or methods opf that cals( some
waht like wrapping)
Encapsulation: Hiding data (attributes of a class from
other)by eclaring private , public etc
Inheriatance: Creating new class from patrent class. As
Mammal class is inherited from Animal Class again Human
being is achild class of Mammal
polymorphism: method overloading, virtual functions  |
| Pratap557 |
| |
| |
| Question |
What is polymorphism ? Explain with examples |
Rank |
Answer Posted By |
|
Question Submitted By :: Wilbur J. Pereira |
| This Interview Question Asked @ Ness-Technologies |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | Polymorphism means one name,many properties.Example of
polymorphism is function overloading whare we can have many
functions with same name but having different properties
like the number of arguments in the function header.  |
| Nisha |
| |
| |
| Answer | The previous example is overloading not polymorphism.
Polymorphism is multiple classes having the same method -
for example - a DOG and CAT class that are sub classes of
ANIMAL - ANIMAL has a virtual function - SPEAK. DOG
implements speak via System.out.println("WOOF") and CAT
implements it as System.out.println("MEOW") then
ANIMAL anim = null;
anim = New DOG();
anim.speak();
anim = New CAT();
anim.speak()
will each put out the appropriate string.  |
| Danneel |
| |
| |
| Answer | polymorphism means many forms,greekword-poly-many fuction
overloding,fuction overloding,operator overloding.
ex->'+'
this opertor can be overloaded to add strings
(concatenation)or sets(union)and so on. functions also can
be overloaded as show in the next sectin.the
terms 'Overloading'and 'polymorphism' are used
interchangeablely.  |
| Ashok Kumar |
| |
| |
| Answer | Polymorphism means one in many forms.It describes a method
in different characteristics. A method gets differentiated
by its Function signature. Function signatures are
datatypes, Number of parameters used in method. For
example, a method called Calculation has different
character such as add, sub, mul, div, etc.  |
| Selvi |
| |
| |
| Answer | Polymorphism means "A single function which is used for
different purposes depending upon the parameters passed"
for eg;
Area is the function name which is used for
calculating areas of the rectangle, triangle and even more.
from this persepective we conclude that area is the single
function which is used for calculating different ones.
area(a);---> Area of the square
area(l,b);---> Area of the rectangle.  |
| K.r.sukumar |
| |
| |
| Answer | POLYMORPHISM is derived from two latin words poly(means-
many) and morphs(means-forms).this concept of OOPS provides
one function to be carried out in several ways or on
several object types.
working:-The polymorphism is the ability of responding
different object in there own way to a particular
message.so,when message is sent requesting an object to do
particular function,the message names the function the
object should perform.beacause diffrent objects can have
different functions with same name,the meaning of the
message must be decided with respect to the particular
object that recieved the message.so,the same message sent
to two different objects can invoke two different functions.
example:-If a brazilian is commanded to speak(),he/she may
speak portuguese. However, if a indian is commanded to speak
(), he/she may speak hindi. They both inherit speak() from
human, but their Subclass methods override the methods of
the Superclass; this is Overriding Polymorphism and
Inheritance. Adding a walk class to human would give both
indian and brazilian object's the same walk method.
// Assembly: Common Classes
// Namespace: CommonClasses
public interface Ihuman
{
string Name
{
get;
}
string Talk();
}
// Assembly: human
// Namespace: human
public class humanBase
{
private string _name;
AnimalBase(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
}
}
// Assembly: human
// Namespace: human
public class indian : humanBase, Ihuman
{
public indian(String name) :
base(name)
{
}
public string Talk() {
return "hindi!";
}
}
// Assembly: human
// Namespace: human
public class brazil : humanBase, Ihuman
{
public brazil(string name) :
base(name)
{
}
public string Talk() {
return "portuguese";
}
}
// Assembly: Program
// Namespace: Program
// References and Uses Assemblies: Common Classes, human
public class Testhuman
{
// prints the following:
//
// ram: hindi!
// Mr. harsh: hindi!
// Lara: portuguese!
//
public static void Main(String[] args)
{
List<Ihuman> human = new List<Ihuman>();
human.Add(new indian("ram"));
human.Add(new indian("Mr. harsh"));
human.Add(new brazilian("Lara"));
foreach(Ihuman human in human)
{
Console.WriteLine(human.Name + ": " +
human.Talk());
}
}
}  |
| Manjeet |
| |
| |
| Question |
What is the difference between and interface and an
abstract class ? |
Rank |
Answer Posted By |
|
Question Submitted By :: Wilbur J. Pereira |
| This Interview Question Asked @ Ness-Technologies , Ness Technologies |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | What is an Interface?
An interface is a contract, a specification that concrete
classes MUST follow. It defines method signatures but
cannot have any implementations; the latter must be
provided by the classes that implement the interface.
C# differs from C++ in this regard because C++ lacks native
language support for interfaces. As a C++ programmers you
have to create an interface by defining an abstract class
with pure virtual methods.
what is an abstract class.................
An Abstract class lets you define some behaviors and force
your subclasses to provide others.
For example, if you have an application framework, an
abstract class may provide default services such as event
and message handling. Those services allow your application
to plug in to your application framework. However, there is
some application-specific functionality that only your
application can perform. So instead of trying to define
these behaviors, the abstract class can declare abstract
methods.
Differences between Interfaces and Abstract classes Which
we use ?
I. multiple inheritance
A class may implement several interfaces but can only
extend one abstract class.
II. default implementation
An interface cannot provide any code at all, much less
default code. An abstract class can provide complete code,
default code, and/or just stubs that have to be overridden.
III. adding functionality
If you add a new method to an interface, you must track
down all implementations of that interface in the universe
and provide them with a concrete implementation of that
method.
If you add a new method to an abstract class, you have the
option of providing a default implementation of it. Then
all existing code will continue to work without change.
IV. is-a vs -able or can-do
Interfaces are often used to describe the abilities of a
class, not its central identity, e.g. an Automobile class
might implement the Recyclable interface, which could apply
to many otherwise totally unrelated objects.
An abstract class defines the core identity of its
descendants.
********************************************************
There are lost of discussion on the internet about the
Interface vs Abstract class. Also, as base class whether we
have to use interface, abstract class or normal class.
I am trying to point out few considerations on which we can
take decision about Interface vs Abstract class vs Class.
Abstract Class vs Interface
I am assuming you are having all the basic knowledge of
abstract and interface keyword. I am just briefing the
basics.
We can not make instance of Abstract Class as well as
Interface.
Here are few differences in Abstract class and Interface as
per the definition.
Abstract class can contain abstract methods, abstract
property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but
we don?t need to put abstract and public keyword. All the
methods and properties defined in Interface are by default
public and abstract.
//Abstarct Class
public abstract class Vehicles
{
private int noOfWheel;
private string color;
public abstract string Engine
{
get;
set;
}
public abstract void Accelerator();
}
//Interface
public interface Vehicles
{
string Engine
{
get;
set;
}
void Accelerator();
}
We can see abstract class contains private members also we
can put some methods with implementation also. But in case
of interface only methods and properties allowed.
We use abstract class and Interface for the base class in
our application.
This is all about the language definition. Now million
dollar question.
Thanks
Manoj(InfoAxon Technology Limited)
 |
| Wilbur J. Pereira |
| |
| |
| Answer | ABSTRACT CLASS Interface
It consist of Static It consist of non- static member
member variables variables
It consist of non-abstract It consist of only abstract
and abstract methods methods
it consist of private and It consist of public classes
protected classes
It uses the keyword Extends uses the keyword Inplements
If subclass extends the If the subclass implements
abstact class, cannot extend interface, can implement any
any other class number of interfaces  |
| Sathish Kumar.t |
| |
| |
| Question |
#include <stdio.h>
#define sqr(x) (x*x)
int main()
{
int x=2;
printf("value of x=%d",sqr(x+1));
}
What is the value of x?
|
Rank |
Answer Posted By |
|
Question Submitted By :: Mittal |
| This Interview Question Asked @ Oracle , Opera |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | 5  |
| Vijaya |
| |
| |
| Answer | Answer is 5  |
| Azeem Khan |
| |
| |
| Answer | the x itself is 2 and the print out is 9  |
| Guest |
| |
| |
| Answer | i tried in pc the ans is 5  |
| Guest |
| |
| |
| Answer | 27  |
| Anil |
| |
| |
| Answer | ouput :value of x=5
bcoz sqr(x+1)=(x+1*x+1) if u substitute x=2 u will get 5
since '*' is having more priority than '+'
at the of prog if u add prinf("%d",x); u will get 2
bcoz x value is not changed  |
| Divakar |
| |
| |
| Answer | absolutely 5  |
| Bhagya |
| |
| |
| Answer | 5
Bcz 2+1*2+1=5  |
| Ndarvind |
| |
| |
| Answer | Ans : 5
I agree with Divakar ans & similar answers.
sqr(x+1)=x+1*x+1=2+1*2+1=5, but not 2*2+1  |
| Gg |
| |
| |
| Question |
Go through the following code sinippet
char a[20];
a="Hello Orcale Test";
will this compile?
|
Rank |
Answer Posted By |
|
Question Submitted By :: Mittal |
| This Interview Question Asked @ Oracle |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | No...
Compile time error will occur says
"left operand must be l-value"
alternatively
char *a;
a="Hello Orcale Test";
will compile....  |
| Jaisai |
| |
| |
| Answer | Compile time err wil occur;
We can use *a="Hello Orcale Test" or a[20]="Hello Orcale
Test";
It will lead the prg nice.  |
| Vikraman.j |
| |
| |
| Question |
what about "char *(*(*a[])())();"
|
Rank |
Answer Posted By |
|
Question Submitted By :: Mittal |
| This Interview Question Asked @ Oracle |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | Simply it is a string.Bze in C the contents within "" will
treate as strings.  |
| Guest |
| |
| |
| Answer | "char *(*(*a[])())();"
is a string, but
char *(*(*a[])())();
is NOT.
It is simply a function pointer declaration, which says :
"declare an array named 'a', of pointers to functions that
take no arguments and return a pointer to a function that
takes no arguments and returns a pointer to char"...thew,
reading function pointers is actually kinda complicated.
NOTE: char *(*(*a[])())(); is an erroneous declaration, for
it to be proper, you have to specify array size, eg:
char *(*(*a[5])())(); will work.  |
| Jaroosh |
| |
| |
| Question |
Will Macros support multiple arguments ?
|
Rank |
Answer Posted By |
|
Question Submitted By :: Mittal |
| This Interview Question Asked @ Oracle |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | yes they will support multiple arguments  |
| Phani Kumar S |
| |
| |
| Answer | Phani Kumar S. Please give me the detailed answer not only
in Yes or No thanks
Plz correct me if i m wrong...
Regards..
Lokesh Kumar Chauhan
lokesh_kumar_chauhan@yahoo.com
Noida  |
| Lokesh Chauhan |
| |
| |
| Answer | no,
macros dont support multiple arguments..  |
| Shruti |
| |
| |
| Answer | yes they will support multiple arguements
they are the short forms for activate some internal
functions  |
| Phani Kumar Satpathi |
| |
| |
| Answer | variadic macros --- C99 standard
#define MY_MACRO(...)  |
| Ronald |
| |
| |
| Question |
Why the use of alloca() is discouraged?
|
Rank |
Answer Posted By |
|
Question Submitted By :: Mittal |
| This Interview Question Asked @ Oracle |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | Hi All,
If you use alloca inside a function when it retuns from
function it will be resulting in memory leak. Thats why
its discouraged to use.
Thanks & Regards
Sathish Kumar  |
| Sathish Kumar |
| |
| |
| Answer | Sorry, that is not strictly correct. According to the man page:
"The alloca() function allocates size bytes of space in the
stack frame of the caller, and returns a pointer to the
allocated block. This temporary space is automatically freed
when the caller returns."
Now this is the real reason:
" If the allocated block is beyond the current stack limit,
the resulting behavior is undefined."  |
| Johnson |
| |
| |
| Question |
what are brk, sbrk?
|
Rank |
Answer Posted By |
|
Question Submitted By :: Mittal |
| This Interview Question Asked @ Oracle |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | These are the system calles used to allocate the memory.
brk will call internally when u call malloc func.  |
| Guest |
| |
| |
| Question |
What is the Difference between Macro and ordinary
definition? |
Rank |
Answer Posted By |
|
Question Submitted By :: Guest |
| This Interview Question Asked @ Motorola , Cognizant, Robert Bosch |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | 1. Macro takes parameters where as ordinary definition does
not.
2. Based on the parameter values to macro it can result in
different value at run time. Ordinary defination value
remains same at all place at run time.
3. Macro can be used for conditional operations where as
definition can not.
4. Using macro one can achieve inline functionality in C
ie. macro can be a function performing simple operations.
This is not possible using definitions.  |
| D G Patel |
| |
| |
| Answer | macro definitions can be used for conditional compilation
whereas ordinary cannot  |
| Himanshu Goel |
| |
| |
| Question |
What is the Difference between Class and Struct? |
Rank |
Answer Posted By |
|
Question Submitted By :: Guest |
| This Interview Question Asked @ Motorola |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | by default
struct elements are public
while
class elements are private  |
| Ashish Srivastava |
| |
| |
| Answer | Class elements are accessed using the provided methods.  |
| Wfwre |
| |
| |
| Answer | no functions in structures
member functions are available in class  |
| Revathy |
| |
| |
| Answer | The only difference between class and struct is that
in class all members are private by default whereas in
struct the members are by default public.  |
| Manju |
| |
| |
| Answer | 1.the variables of struct r jus varialbles inly,whereas it
it objects for class...
2. pointers can be used in structure only for same return
types,but in classes it can be uses for diff classes  |
| Vishnupriya |
| |
| |
| Answer | answer 3 is wrong.
answer 4 is absolutely corret.  |
| Purna |
| |
| |
| Answer | the diff b/w structure and class is class by deafualt
private, where as structure by default public.  |
| Sureshreddy |
| |
| |
| Answer | STRUCTURE CLASS
1.structure by default is 1. class by default is private
public.
2.structure does not 2.class provide data hiding.
provide data hiding.
3.A structure would be the 3.class would be the collection
collection of related data. of data & code which handels
data.
CAN U TELL ME MORE DIFFERENCE BETWEEN LIKE THIS PLZ TELL ME
AS EARLY AS POSSIBLE I AM WAITING  |
| Satvir Kaur |
| |
| |
| Answer | Structs allocates continues memory
where as class does not
structs and functions are the basic idea behind class  |
| Santhosh.r |
| |
| |
| Question |
How to reverse a string using a recursive function, without
swapping or using an extra memory? |
Rank |
Answer Posted By |
|
Question Submitted By :: Guest |
| This Interview Question Asked @ Motorola , Cisco |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | /* Following code does as intended */
#include <stdio.h>
#define REVERSE_STRING(X) Rstring(X, *(X), strlen(X)-1)
void Rstring( char *str, char c, int index )
{
if( index != 0 )
Rstring( str, *(str+(strlen(str))-index),
index-1);
*(str+index) = c;
}
int main( void )
{
char str[] = "Dharmendra Patel";
printf("Actual string is [%s]\n", str);
REVERSE_STRING(str);
printf("Reversed string is [%s]\n", str);
return 0;
}  |
| D G Patel |
| |
| |
| Answer | void reverse(char *,int b);
void main()
{
char a[26];
int len;
clrscr();
printf("enter string ");
gets(a);
len=strlen(a);
reverse(a,len);
getch();
}
void reverse(char * a,int len)
{
if(len==0)
printf("%c",a[len]);
else
{
printf("%c",a[len]);
reverse(a,len-1);
}
}  |
| Vinay Tiwari |
| |
| |
| Answer | void Rstring( char *str,int len)
{
for(int i = 0; i < (len/2); i++)
{
str[i] ^= str[len-i-1];
str[len-i-1] ^= str[i];
str[i] ^= str[len-i-1];
}
}
int main( void )
{
char str[] = "my string";
printf("Actual string is [%s]\n", str);
Rstring(str,strlen(str));
printf("Reversed string is [%s]\n", str);
}
I dont call this swaping, coz it's not, recursive creates
new incarnations of the reverse func, EXTRA MEMORY BIG
TIME!!!  |
| Boomer |
| |
| |
| Answer | #include <iostream>
#include <conio>
void reverse(char a[], int s, int sc );
void reverse(char a[], int s, int sc ){
if ((sc-s)<(s-1))
{
a[sc-s]^=a[s-1];
a[s-1]^=a[sc-s];
a[sc-s]^=a[s-1];
reverse (a, s-1, sc) ;
}
}
void main (){
char a[]="ABCDEFG";
reverse(a, 7, 7);
cout<<a;
getch(); //i just use it to freeze the screen
}  |
| Moinom |
| |
| |
| Answer | #include <iostream>
using namespace std;
char* reverse_str(char* s)
{
char* reverse = new char[1];
//char* reverse;
int i;
if(*s != '\0')
reverse = reverse_str(s+1);
i = strlen(s) - 1;
if (i >= 0)
reverse[i] = s[0];
return reverse;
}
int main(void)
{
char* str = "tsirhc oraivur odraude leafar";
cout << "original:" << endl;
cout << str << endl << endl;
cout << "reversed:" << endl;
cout << reverse_str(str) << endl;
return 0;
}  |
| Rafael Christ |
| |
| |
| Answer | /*
reverse string between start and end indexes of a string
*/
void reverse( char* str, int start, int end )
{
if( str && ( start < end ) )
{
*( str + start ) ^= *( str + end ) ^= *( str + start )
^= *( str + end ) ;
reverse( str, ++start, --end );
}
}
int main()
{
char sample[] = "My String!";
reverse( str, 0, strlen( sample )-1 )
}  |
| Pritam |
| |
| |
| Answer | #include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{
char str1[] = "Mahesh";
char str2[80], *p1, *p2;
clrscr();
p1 = str1 + strlen(str1) - 1;
p2 = str2;
while(p1 >= str1)
*p2++ = *p1--;
*p2 = '\0';
printf("%s %s", str1, str2);
getch();
return 0;
}  |
| Mahesh Auti |
| |
| |
| Answer | #include<stdio.h>
#include <string.h>
char * reverse (char *); //function prototype
int length(char *); //function prptotype
void main()
{
int i;
char *str, *rev;
clrscr();
gets(str);
strcpy(rev,reverse(str));
printf("Original %s Reverse %s", str, rev);
free(str);
free(rev);
getch();
}
int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}
char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';
printf("\nOUT: %s\n", t);
return t;
}  |
| Mahesh Auti |
| |
| |
| Answer | Reverse a string
void ReverseString (char *String)
{
char *Begin = String;
char *End = String + strlen(String) - 1;
char TempChar = '\0';
while (Begin < End)
{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End--;
}
}  |
| Mahendra Aseri |
| |
| |
| Answer | Using recursive Function:
void rev_str(char *str)
{
if(*str!=NULL)
rev_str(str+1);
printf("%c",str);
}  |
| Mahendra Aseri |
| |
| |
| Answer | #include <iostream>
using namespace std;
void rev_str(char* str, int pos=-1, char c='\0'){
if(pos >= int(strlen(str)/2))
return;
if(c != '\0'){
str[strlen(str) - pos - 1] = str[pos];
str[pos] = c;
}
rev_str(str, ++pos, str[strlen(str) - pos - 2]);
}
int main(){
char str[] = "reverse this string";
cout << str << endl;
rev_str(str);
cout << str << endl;
//:~
return 0;
}  |
| Smbrd |
| |
| |
| Answer | #include <iostream>
#include <string>
using namespace std;
char * reverse (char *); //function prototype
int length(char *); //function prptotype
int main()
{
int i;
char *str = new char[6], *rev = new char[6];
cin >> str;
strcpy(rev,reverse(str));
cout <<"Original "<< str << " reverse " << rev << endl;
free(str);
free(rev);
return 0;
}
int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}
char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
t = new char[n];
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';
return t;
}
/*based off of answer 8 i took this an intialized the
pointers so that it would run, and switched it over to the
C++ standard output commands. His algorithm was correct, he
just forgot to setup the memory*/  |
| Stephen |
| |
| |
| Answer | #include <iostream>
#include <string>
using namespace std;
char * reverse (char *); //function prototype
int length(char *); //function prptotype
int main()
{
int i;
char *str = new char[6], *rev = new char[6];
cin >> str;
strcpy(rev,reverse(str));
cout <<"Original "<< str << " reverse " << rev << endl;
free(str);
free(rev);
return 0;
}
int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}
char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
t = new char[n]; //opps have to add 1 here or there
wont be room for a null!
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';
return t;
}
//can only handle words 5 letters or less.
/*based off of answer 8 i took this an intialized the
pointers so that it would run, and switched it over to the
C++ standard output commands. His algorithm was correct, he  |
| Stephen |
| |
| |
| Answer | #include <stdio.h>
void reverse(char *str)
{
if (*str == '\0')
return;
reverse(str+1);
printf("%c", *str);
}
int main()
{
char str[50];
printf("Enter the string: ");
scanf("%s", str);
printf("Reversed string: ");
reverse(str);
printf("\n");
return 1;
}  |
| Prakash |
| |
| |
| Answer | Another version that actually reverses the string...
#include <stdio.h>
char *reverse(char *sstr, char *str, char c)
{
if (*str == '\0')
return sstr;
sstr = reverse(sstr, str+1, *(str+1));
*sstr = c;
return (sstr+1);
}
int main()
{
char str[100];
printf("Enter the string: ");
scanf("%s", str);
reverse(str, str, *(str + 0));
printf("Reversed string: %s\n", str);
return 1;
}  |
| Prakash |
| |
| |
|
| |
|
Back to Questions Page |