No, C is its own language. However, C++ is another language that is based on C, but isn't really a "superset" of it. C++ introduces object-oriented-programming, which facilitates development much more easily than the original C. In fact, there are a whole class of languages based on C, including C# and C++. Note: The (ancient) predecessor of C was B.
There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program.
A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome.
The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes.
The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string.
The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not.
The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in.
You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string input_string(string prompt)
{
cout<<prompt<<":\t";
string input;
getline(cin, input, '\n');
return(input);
}
void convert_tolower(string& s)
{
for(string::iterator i=s.begin(); i!=s.end(); ++i)
*i=tolower(*i);
}
vector<int> strip(const string& s)
{
vector<int> v;
v.push_back(-1);
for(int i=0; i<s.size(); ++i)
{
if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9'))
{
v.push_back(i);
v.push_back(-1);
}
}
return(v);
}
bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y)
{
for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y)
if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] ))
break;
++x, --y;
if( v[x]==-1 )
++x, --y;
return(x>=0 && x<y && y-x>1);
}
int main()
{
string input;
while(1)
{
input=input_string("Enter a string");
if(input.size()==0)
break;
string copy(input);
convert_tolower(copy);
vector<int> v=strip(copy);
string pal;
int pos=0;
for(int i=0; i<v.size(); ++i)
{
int start=0, end=0;
if( ispalindrome( copy, v, i, start, end))
{
string tmp( input.substr(v[start],v[end]-v[start]+1));
if( tmp.size() > pal.size() )
{
pal = tmp;
pos = v[start];
}
}
}
if( pal.size() )
{
cout<<"Palindrome:\t";
for(int i=0; i<pos; ++i)
cout<<" ";
cout<<pal<<"\n"<<endl;
}
else
cout<<"The string contains no palindromes!\n"<<endl;
}
return(0);
}
Example output:
Enter a string: Madam, I'm Adam
Palindrome: Madam, I'm Adam
Enter a string: Madam, I'm Adam is a palindrome
Palindrome: Madam, I'm Adam
Enter a string: In girum imus nocte et consumimur igni
Palindrome: In girum imus nocte et consumimur igni
Enter a string: 0123456765432
Palindrome: 23456765432
Enter a string:
Press any key to continue . . .
How do you swap 2 variables without using third variable?
void main() { int a,b; clrscr(); printf("\n\n\t\tenter any two nos..."); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("\n\n\t\tvalue of a=",a); printf("\n\n\t\tvalue of b=",b); getch(); }
What is the difference between initialisation and definition in C programming?
Variable-declaration is:
extern int x;
extern double y;
extern char a;
Variable-definition is:
int x;
static double y;
auto char a;
Variable-definition with initialization is:
int x = 1;
static double y= 2.3;
auto char a = 'w';
Define polymorphism in c plus plus?
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
How do you convert from assembly to binary in c plus plus?
Use inline assembly instructions. Then compile your C++ program to produce the machine code.
What is cputs function in computer c plus plus?
Nothing.
The C language only recognizes a few keywords, like "for" and "if". Most of what's in a C program ... that doesn't reference routines in the C program itself ... are library calls, and cputs() is one of those. What it does is write its argument (which should be a pointer to a character string) to the console... console put string.
C plus plus program for sum of n numbers using class?
#include <iostream>
int main()
{
double num1 = 0.0;
std::cout << "Enter first number: ";
std::cin >> num1;
double num2 = 0.0;
std::cout << "Enter second number: ";
std::cin >> num2;
std::cout << num1 << " + " << num2 << " = " << (num1 + num2);
return 0;
}
HOW to display array elements C plus plus?
In C++, it is better to use vectors than dynamic arrays because a vector always knows its own size. C-style arrays are better suited to static arrays where the size is always known in advance. The following demonstrates how a vector might be used within a class.
#include<iostream>
#include<vector>
class foo
{
public:
std::vector<int>& operator+= (int data){ m_data.push_back(data); return(m_data);}
int operator[] (size_t element)const{
ASSERT(element<m_array.size());
return( m_array[element]; }
const size_t size()const{return(m_data.size());}
private:
std::vector<int> m_data;
}
int main()
{
foo f;
f += 42;
f += 9;
for(int i=0; i<f.size(); ++i)
std::cout<<f[i]<<std::endl;
}
Output:
42
9
How do you make C plus plus Program Autorun?
This is not a C++ question. It is an operating system question. Different operating systems have different ways of making programs run on startup. Most of them have several different ways to accomplish this. In MS Windows, one way is to place a shortcut for the program in the "{percent}userprofile{percent}\Start Menu\Programs\Startup" shell folder.
What is the difference between C plus plus and MS Visual C plus plus computer programming?
C++ is simply the generic implementation, based upon the version originally developed by Bjarne Storustrup, and which is considered the standard implementation. Visual C++ is Microsoft's implementation of the language, which follows much of the standard, but is not 100% compliant. However, VC++ includes an extensive and exclusive library that is specific to Windows programming. Competitors such as Embarcadero's C++ Builder have similarly extensive Windows libraries but which are not compatible with Microsoft's.
Can you have inline virtual functions in a class?
No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.
Write the algorithm to find the largest number of three number?
To determine the largest of any group of numbers, we must first determine the largest of any two numbers. For that we use the following simple algorithm:
If number A is greater than number B, then return number A otherwise return number B.
In C++ we can encode this algorithm using the following template function:
T& max( T& a, T& b) { return( a>b?a:b ); }
Using this one basic function we can build more complex functions to return the largest of any quantity of numbers. For instance, to return the largest of any three numbers, we would use the following:
T& max( T& a, T& b, T& c ) { return( max( max( a, b ), c )); }
To return the largest of any four numbers, we would use the following:
T& max( T& a, T& b, T& c, T& d) { return( max( max( a, b, c ), d )); }
We could continue in this fashion, but there is a much simpler approach: place all the values in a vector and return the maximum value of the vector. The following example demonstrates this with a vector containing 10 random values:
#include
#include
#include
template
T& max(T& a, T& b ) { return( a>b?a:b ); }
template
T max( std::vector
{
std::vector
int iResult = *it;
while( ++it != v.end() )
iResult = max( iResult, *it );
return( iResult );
}
int main()
{
srand(( unsigned ) time( NULL ));
std::vector
printf( "Array: " );
for( std::vector
{
*it = rand() % 100 + 1;
printf( "%d ", *it );
}
printf( "\n");
printf( "Largest: %d\n", max(v) );
return( 0 );
}
How do you convert postfix expression to prefix expression using c plus plus?
Let Q be an arithmetic expression written in infix notation. Besides operands and operators, Q may also contain left and right parentheses. We assume that the operators in Q consist only of exponentiations (↑), multiplications (*), divisions (/), additions (+) and subtractions (-), and that they have the usual three levels of precedence as given above. We also assume that operators on the same level, including exponentiations, are performed from left to right unless otherwise indicated by parentheses. (This is not standard, since expressions may contain unary operators and some languages perform the exponentiations from right to left. However, these assumptions simplify our algorithm.)
The following algorithm transforms the infix expression Q into its equivalent postfix expression P. The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using the operands from Q and the operators, which are removed from STACK. We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q. The algorithm is completed when STACK is empty.
69
Algorithm: POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.
1. Push "(" onto STACK, and add ")" to the end of Q.
2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator ⊗ is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on
the top of STACK) which has the same precedence as or higher precedence than ⊗.
(b) Add ⊗ to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on
the top of STACK) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
[End of If structure.]
[End of Step 2 loop.]
7. Exit.
The terminology sometimes used for Step 5 is that ⊗ will "sink" to its own level. .
70
EXAMPLE
Consider the following arithmetic infix expression Q:
Q: A+(B*C- (D/E↑F)*G)*H
We simulate the previous algorithm to transform Q into its equivalent postfix expression P.
First we push "(" onto STACK, and then we add ")" to the end of Q to obtain:
A + ( B * C - ( D / E ↑ F ) * G ) * H )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)(14) (15) (16)(17) (18)(19) (20)
The elements of Q have now been labeled from left to right for easy reference. Table below shows the status of STACK and of the string P as each element of Q is scanned. Observe that
(1) Each operand is simply added to P and does not change STACK.
(2) The subtraction operator (-) in row 7 sends * from STACK to P before it (-) is pushed onto STACK.
(3) The right parenthesis in row 14 sends j and then I from STACK to P, and then removes the left parenthesis from the top of STACK.
(4) The right parenthesis in row 20 sends * and then + from STACK to P, and then removes-the left parenthesis from the top of STACK.
After Step 20 is executed, the STACK is empty and
P: A B C * D E F ↑ / G * - H * +
71
which is the required postfix equivalent of Q.
How to convert 2D array to single dimensional array?
You don't need to physically convert the array, you simply need to point at the first element and read the elements sequentially. The following code shows how to achieve this, as well as how to physically convert a 2D array to a separate 1D array. As can be seen, there is no practical benefit to making the conversion.
Example
#include
int main()
{
const int rows = 2;
const int cols = 3;
const int size = rows * cols;
// Create a static 2D array, intialise and print the matrix
int arr2[rows][cols];
std::cout << "2D array:\n" << std::endl;
for( int r=0; r { for( int c=0; c { arr2[r][c]= (r+1) * (c+1); std::cout << arr2[r][c] << " "; } std::cout << std::endl; } std::cout << std::endl; // Efficient conversion to 1D array: // Point to the first element then print sequential values. int* ptr = (int*) arr2; std::cout << "1D array (using pointer):\n" << std::endl; for( int i=0; i std::cout << *ptr++ << " "; std::cout << "\n" << std::endl; // Inefficient conversion to 1D array: // Copy the 2D array to a separate 1D array int arr1[size]; ptr = (int*) arr2; for( int i=0; i arr1[i]=*ptr++; // Print the copy: std::cout << "1D array (using copy):\n" << std::endl; for( int i=0; i std::cout << arr1[i] << " "; std::cout << "\n" << std::endl; return( 0 ); } Output 2D array: 1 2 3 2 4 6 1D array (using pointer): 1 2 3 2 4 6 1D array (using copy): 1 2 3 2 4 6
Multithreaded program generates Fibonacci series using java-answer?
The formula for the Fibonacci series is
Fn = Fn-1 + Fn-2 for n ≥ 2 with F0 = 0 and F1 = 1.
In layman's terms, in the Fibonacci series each successive number is the sum of the previous two numbers, starting with 1. So we have 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc
To do this in Java or any computer program you will need to create a recursive program, one that picks up previous values. This is quite easy, even for a beginner, once one grasps the Fibonacci fundamentals. Here is an example in Java:
public class Fibonacci {
public static long fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}
}
For n, enter the number of values required, otherwise the increasing values of the results will overflow the parameters of the data display!
This code can be adapted for any progamming language, e.g. pascal, basic etc. One can even use the principles to construct a spreadsheet that will show the Fibonacci series in successive rows in Excel, for example.
What is an object and how are they created in object oriented programming languages?
Objects are the examples of the idea which the class represents. Objects of a class are constructed using the "new" keyword. For example: class Person { String name; } class Earth { public static void main(String... args) { Person sunny = new Person(); // Creating Object sunny.name = "Sunny"; } } Here, we have a class Person, the object of this class (in this case a person having name Sunny) is created from another class Earth using the keyword "new".
Write a program using do while loop?
char YorN; //I named this after Yes or No
do
{
cout<< "Do you want to continue (Y/N)? "; //You have to answer this with a 'Y' or 'N' to continue
cin >> YorN;
}
while((YorN !='Y')&&(YorN !='N')&&(YorN !='y')&&(YorN !='n')); //This ends the program when the user has -
entered either of these characters: 'Y', 'y', 'N', 'n'.
//The while keeps activating until YorN equals 'Y', 'y', 'N', 'n'.
I hope this has helped you! :D
What are the basic input and output of c and c plus plus?
That is STANDARD input and STANDARD output.
By default, standard input is the keyboard, and standard output is the screen. Standard I/O is set by the operating system, though it may be redirected by script invocation or system commands within the C/C++ program itself. You could, for instance, set standard output to a printer or a file in lieu of a screen.
You should also Google Standard Error.
How do you write a function in Turbo C Plus Plus?
// declaration:
return_type function_name([argument_type_1[=value][, argument_type_2[=value][, ...]]]);
// definition (function signature must match declaration):
return_type function_name([argument_type_1 argument_name_1[, argument_type_2 argument_name_2[, ...]]])
{
// implementation
}
In the case of separate declaration/definition, the definition file must include the file that contains the declaration unless they are both in the same file. If the function is a template function, both the declaration and the definition must be visible to the compiler BEFORE it can be used (thus they must both be in the same file). For all other functions other than inline expanded functions, only the declaration need be visible.
Note that the definition is also a declaration, however default values must be omitted and all arguments must be named. The declaration arguments may also be named but they needn't match those in the definition (the definition names are the ones actually used).
Alternatively:
// combined declaration and definition:
return_type function_name([argument_type_1 argument_name_1[=value][, argument_type_2 argument_name_2[=value][, ...]]])
{
// implementation
}
Functions that are defined in the declaration are impicitly inline expanded. Functions that are defined separately must be prepended with the inline keyword in the definition, and the definition must be visible to the compiler BEFORE the function can be used.
Functions that do not return a value must return void. If any other return type is specified, the function must explicitly return that type via all return paths. The main function must return an int, however return(0) is implied if not specified in the final statement.
Object files are intermediate files generated when an application is constructed from source code. In a classic compile-assemble-link toolchain, the compiler translates (C language) source code into assembly language output. The assembler translates this intermediate assembly source into a binary form known as object code. Files containing object code often have a .o or .obj file name extension and are generally called object files. The linker takes the object files and combines them into the complete executable.
While many modern tools hide the individual steps, or even integrate compilation and assembly into one, most (all?) compilers generate object files.
Derived data is data that is copied or enhanced from operational data sources into an informational database. This is in the information catalog center.
What is an object in C plus plus?
In c++
object is an instance of a class.class is an abstract data type.abstract data type means humen defined data type which is created in order to full fill the requirements of the problem.once we create a class ,we can have as many objects (of that class )as we want.
Difference between member functions and static member functions?
c: A static function is visible only in the source file containing it. A static member is not defined. c++: A static function is visible only in the source file containing it. A static member variable is common to all instances of a class. A static member function is used to access static members.
Is there any other keyword like goto in c plus plus?
The goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label. The following is an example of a goto statement and statement label:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
tryAgain: // this is a statement label
cout << "Enter a non-negative number";
double dX;
cin >> dX;
if (dX < 0.0)
goto tryAgain; // this is the goto statement
cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl;
}