Program to check whether a number is a palindrome or not using functions?
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 . . .
This depends upon what "base" you are referring. In "base 10", 111 is 1101111.
In "base 8", 111 is 1001001
In "base 16", 111 is 100010001.
Of course, if you are just asking "what is the value of 111 in base 10" that would be 7. (4 + 2 + 1)
To get you started, you need a simple conversion function to convert inches to millimeters. Here's an example using the literal constant 25.4:
constexpr double convert_inch_to_mm (const double inches) {return inches * 25.4;}
What is the language processor?
Language processors are language translation software like assembler, interpreter and compiler
Why is it important to have a solid background in mathematics for programming languages?
You need to to know some basic math to live. right? Likewise you need to know some basic math to start computer programming....beginners start out by learning to do programs like solving a quadratic equation....if you are not thorough with algebra, u can't do it. The way you think is more important.....rather than being good in math, computer programmers are required to think the way math people think - vey very very logically.
A multithreaded program that generates the Fibonacci series using Pthreads?
#include<stdio.h>
#include<time.h>
#include<pthread.h>
#include<stdlib.h>
#include<sys/types.h> /* need to calculate which I will implement later */
void *fibr(void *n);
void *fibr_1(void *k);
signed long long int fibonacci(signed long long int);
int main(){
clock_t begin, end;
double time_spent;
pthread_t tid,tid1;
int result,result1;
signed long long int n=6;
signed long long int m=7;
result=pthread_create(&tid,NULL,fibr,&n);
if(result){
perror("pthread_create");
return 1;
}
result1=pthread_create(&tid1,NULL,fibr,&m);
if(result1){
perror("pthread_create");
return 1;
}
if(pthread_join(tid,NULL)){
perror("pthread_join");
return 1;
}
if(pthread_join(tid1,NULL)){
perror("pthread_join");
return 1;
}
printf("Fib value=%lld\n",n+m);
pthread_exit(NULL);
}
void *fibr(void *n){
signed long long int *y=n;
signed long long int x=*y;
pthread_t tid2,tid3;
signed long long int i,j;
/* How do I assign values to i , j in order to
achieve the level viz fib(n-2)....fib(n-4) */
if(pthread_create(&tid2,NULL,fibr_1,&i))
{
perror("pthread_create");
}
if(pthread_create(&tid3,NULL,fibr_1,&j))
{
perror("pthread_create");
}
if(pthread_join(tid2,NULL))
{
perror("pthread_join");
}
if(pthread_join(tid3,NULL))
{
perror("pthread_join");
}
/* How to return the values of i, j combined with *y . if I do *y+i+j, the result
is not coming correctly */
*y=fibonacci(x);
return NULL;
}
void *fibr_1(void *k){
long long int *a=k;
long long int b=*a;
*a=fibonacci(b);
return NULL;
}
signed long long int fibonacci(signed long long int x){
if((x==0)(x==1))
return x;
return fibonacci(x-1)+fibonacci(x-2);
}
What is a bit in a computer code?
A bit is the smallest unit of storage in a computer. It represents either a 0 or a 1, which comes from the electrical basis of a computer in which current can either be flowing (1) or not flowing (0).
What are the applications of this pointer?
The this pointer is used to refer to the object that is foremost in the current scope. It has three primary purposes. First, a programmer uses this to refer to a member variable that has been shadowed by a function argument. Secondly, it is used to obtain a reference to itself, usually for the purpose of passing itself to another function or class. Finally, the this pointer can be used to call alternate constructors of the same object, usually as a means of reusing code and providing various constructors with default arguments.
Implement Bresenham algorithm using c language?
#include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> void draw_line(float,float,float,float); main() { int driver,mode; float x1,y1,x2,y2; clrscr(); printf("Enter the two endpoints of the line:"); printf("\nx1 ="); scanf("%f",&x1); printf("y1 ="); scanf("%f",&y1); printf("x2 ="); scanf("%f",&x2); printf("y2 ="); scanf("%f",&y2); clrscr(); driver = DETECT; initgraph(&driver,&mode,"\\tc\\bgi"); \\path of bgi can be different in your case draw_line(x1,y1,x2,y2); getch(); closegraph(); } void draw_line(float x1,float y1,float x2,float y2) { float dy,dx; float x,y; float p,p0,dp1,dp2; dy = y2-y1; dx = x2-x1; p0 = 2 * (dy - dx); dp1 = 2 * dy; dp2 = 2 * (dy - dx); putpixel(x1,y1,EGA_WHITE); p = p0; for(x=x1+1,y=y1;x<x2;x++) { if(p < 0) { p = p+dp1; putpixel(x,y,EGA_WHITE); } else { p = p+dp2; y++; putpixel(x,y,EGA_WHITE); } }
What is mail merge and where is it used?
Mail merge is a facility of Microsoft's Word program, and other wordf processing applications.
It allows the user to set up a form letter, with blank spaces in certain parts.
The user then sets up a mini-database, or uses an existing database, to send a personalised copy of the letter to people in the database, with the blank spaces being filled by their particulars... the process adjusts the text of the letter so that even if the entry is extremely long, or extremely short, the recipient thinks that the letter is unique to them.
It can also be used just to print address labels.
Would a 64Bit processor work with a 32Bit operating system?
It can do but it is really the other way around. A 32-bit operating system can work with a 64-bit processor. However, if the processor doesn't have a "soft" switching mode, you will have to manually switch the 64-bit processor to 32-bit mode via the CMOS setup.
Ideally you should install a 64-bit operating system and leave the processor in 64-bit mode. This will then allow you to run 32-bit programs and 64-bit programs side-by-side.
Write a program to accept two numbers and display their sum?
Assuming you're using the C programming language (as you did not specify any other), and that you're making some sort of arithmetic tester, a simple answer would be:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
int num1 = (rand()%100);
int num2 = (rand()%100);
int sum = num1 + num2;
int ans;
printf("What is %d + %d? ", num1, num2);
scanf("%d", &ans);
if(ans == sum) printf("Correct!");
else printf("Wrong, correct answer is %d", sum);
return 0;
}
Why do the time on my computer changes for no reason?
The reason is your cmos battery is about to die. All desktop PCs use a small lithium battery, similar to those used by hand-held calculators to keep time and date info. Once this battery runs low on voltage, your date and time will be shown incorrectly.
The solution: unplug your PC, open the case and carefully extract the battery (use a small flashlight). Buy a new one of the same number (cr2032, cr2025, etc.) and install it the right way. Don't worry, it will fit only one way. Now turn your PC on and set the time and date and that's it, no problem with that for a good few years. Hope this helps.
What does printf and scanf return?
Printf returned no. of character receive .
scanf return no of variable to be inputed according to format specifier . eg:
i=printf("thisisc") printf("%d",i); //i=7 j=scanf("dd",&a,&b,&c,&d); printf("%d",j);
IT IS MOST APPROPRIATE ANSWER FOR THIS Q .......... PLZ DO Practically............
How many pointers will have to be changed if a node is deleted from a linear linked list?
Can you have a final abstract class?
No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.
Combining them would lead to an unusable class, so the compiler will not let this happen.
What are advantages and disadvantages of priority queue?
In CQ we utilize memory efficiently. because in queue when we delete any
element only front increment by 1, but that position is not used later. so when
we perform more add and delete operation, memory wastage increase. But in CQ
memory is utilized, if we delete any element that position is used later,
because it is circular.
What is the difference between a queue and a stack?
Queue is better than stack because jobs in a queue are processed on a first in first out order thereby reducing traffic and delay.
But jobs in a stack are processed in a last in first out order causing traffic and delay of jobs that arrived earlier
What are 3 different data types?
According to Wikipedia,
"In computer science and computer programming, a data type or simply type is a classification identifying one of various types of data, such as real-valued, integer or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored."
"Almost all programming languages explicitly include the notion of data type, though different languages may use different terminology. Common data types may include:
How do you write algorithm to find the area of a rectangle?
For example: L - Length & W - Width
start
sum = 0
input L, W
sum = L * W
print "The area of the rectangle is: SUM"
end
What commands to draw pentagon in logo?
to draw-pentagon :length
pd
seth 126
repeat 5 [ fd :length rt 72 ]
end
Draws a pentagon "length" pixels on each side, with the turtle originating and terminating at the apex.
What is a computer interpreter?
An interpreter, or a translator, is a person who translates different languages. For an example, if a Chinese person and an American person can't understand each other, an intepreter can translate for both of them.
What is a high level progrmming language?
It is a programming language with strong abstraction from the details of the computer.
Advantages and disadvantages of using employee referral campaign recruitment?
· Employee referrals:
o Advantages: choosing a person that you know well (character, qualifications etc), save cost & time vs advertisement.
o Disadvantages: if person is not suitable (qualifications, experience, character etc) but is merely a friend/relative (nepotism) of the current employee. Favoritisms, potential request for unreasonable favor in return.