Translate

Tuesday, January 14, 2014

Write a program to delete a specified line from a text file.

In this program, user is asked for a filename he needs to change. User is also asked for the line number that is to be deleted. The filename is stored in'filename' . The file is opened and all the data is transferre d to another file except that one line the user specifies to delete.

Program: Program to delete a specific line.

# include int main(){
FILE *fp1, *fp2;
// consider 40 character string to store filename
char filename[4 0];
char c;
int del_line, temp = 1;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in'filename'
scanf("%s" , filename);
//open file in read mode
fp1 = fopen(file name,"r");
c = getc(fp1);
//until the last character of file is obtained
while (c != EOF)
{
printf("%c ", c);
//print current character and read next character
c = getc(fp1);
}
//rewind
rewind(fp1 );
printf("\n Enter line number of the line to be deleted:") ;
//accept number from user.
scanf("%d" ,&del_line) ;
//open new file in write mode
fp2 = fopen("cop y.c","w");
c = getc(fp1);
while (c != EOF){
c = getc(fp1);
if (c =='\n')
temp++;
//except the line to be deleted
if (temp != del_line)
{
//copy all lines in file copy.c
putc(c, fp2);
}
}
//close both the files.
fclose(fp1 );
fclose(fp2 );
//remove original file
remove(fil ename);
//rename the file copy.c to original name
rename("co py.c", filename);
printf("\n The contents of file after being modified are as follows:\n ");
fp1 = fopen(file name,"r");
c = getc(fp1);
while (c != EOF){
printf("%c ", c);
c = getc(fp1);
}
fclose(fp1 );
return 0;
}
Output:
Enter file name:abc.t xt
hi.
Hello
how are you?
I am fine
hope the same
Enter line number of the line to be deleted:4
The contents of file after being modified are as follows:
hi.
hello
how are you?
hope the same
Explanatio n:
In this program, user is asked for a filename that needs to be modified. Entered file name is stored in a char array'filename' . This file is opened in read mode using file pointer'fp1'. Character'c'is used to read characters from the file and print them to the output. User is asked for the line number in the file to be deleted. The file
pointer is rewinded back and all the lines of the file except for the line to be deleted are copied into another file "copy.c". Now"copy.c"is renamed to the original filename. The original file is opened in read mode and the modified contents of the file are displayed on the screen. 

Monday, January 13, 2014

Write a program to compare two strings without using strcmp() function.

strcmp() function compares two strings lexicograp hically. strcmp is declared in stdio.h
Case 1: when the strings are equal, it returns zero.
Case 2: when the strings are unequal, it returns the difference between ascii values of the characters that
differ.
a) When string1 is greater than string2, it returns positive value.
b) When string1 is lesser than string2, it returns negative value.
Syntax:
int strcmp (const char *s1, const char *s2);
Program: to compare two strings.

# include #include
int cmpstr(cha r s1[10], char s2[10]);
int main(){
char arr1[10] ="Nodalo";
char arr2[10] ="nodalo";
printf("%d", cmpstr(arr 1, arr2));
// cmpstr() is equivalent of strcmp()
return 0;
}/
/s1, s2 are strings to be compared
int cmpstr(cha r s1[10], char s2[10]){
//strlen function returns the length of argument string passed
int i = strlen(s1) ;
int k = strlen(s2) ;
int bigger;
if (i<k){
bigger = k;
}
else if (i>k){
bigger = i;
}
else{
bigger = i;
}
//loops'bigger'times
for (i = 0; i<bigger; i++){
// if ascii values of characters s1[i], s2[i] are equal do nothing
if (s1[i] == s2[i]){
}
//else return the ascii difference
else{
return (s1[i] - s2[i]);
}
}
//return 0 when both strings are same
//This statement is executed only when both strings are equal
return (0);
}
Output:
-32
Explanatio n:
cmpstr() is a function that illustrate s C standard function strcmp(). Strings to be compared are sent as arguments to cmpstr().
Each character in string1 is compared to its correspond ing character in string2. Once the loop encounters a differing character in the strings, it would return the ascii difference of the differing characters and exit.

Worlds best 8 Superb Sentences

Shakespeare.
"Never Play With The Feelings Of Others Because You May Win The Game But The Risk Is That You Will Surely Lose The Person For A Life Time".

Napoleon.
"The world suffers a lot. Not because of the violence of bad people, But because of the silence of good people!"

Einstein.
"I am thankful to all those who said NO to me It's because of them I did it myself."

Abraham Lincoln.
"If friendship is your weakest point then You are the strongest person in the world."

Shakespeare.
"Laughing Faces Do Not Mean That There Is Absence Of Sorrow! But It Means That They Have The Ability To Deal With It".

William Arthur.
"Opportunities Are Like Sunrises, If You Wait Too Long You Can Miss Them".

Hitler.
"When You Are In The Light, Everything Follows You, But When You Enter Into The Dark, Even Your Own Shadow Doesn't Follow You."

Shakespeare.
"Coin Always Makes Sound But The Currency Notes Are Always Silent. So When Your Value Increases Keep Quiet. 

Sunday, January 12, 2014

Moment of thinking...

These are few questions asked in HR interview! The answers are really stunning and
inspiring. Thinking out of the box! A must read…

Question 1:
You are driving along in your car on a wild, stormy night, it's raining heavily, when suddenly you pass by a bus stop, and you see three people waiting for a bus:

* An old lady who looks as if she is about to die.

* An old friend who once saved your life.

* The perfect partner you have been dreaming about.

Which one would you choose to offer a ride to, knowing very well that there could only be one passenger in your car?

This is a moral/ethical dilemma that was once actually used as part of a job application.

He simply answered: "I would give the car keys to my Old friend and let him take the lady to the hospital. I would stay behind and wait for the bus with the partner of my dreams."

Sometimes, we gain more if we are able to give up our stubborn thought limitations. Never forget to "Think Outside of the Box."

Question 2:
What will you do if I run away with your sister?
The candidate who was selected answered " I will not get a better match for my sister than you sir"

Question 3:
Interviewer (to a student girl candidate) – What is one morning you woke up & found that you were pregnant.
Girl – I will be very excited and take an off, to celebrate with my husband.
Normally an unmarried girl will be shocked to hear this, but she managed it well. Why I should think it in the wrong way, she said later when asked.

Question 4:
Interviewer: He ordered a cup of coffee for the candidate. Coffee arrived kept before the candidate, then he asked what is before you?
Candidate: Instantly replied "Tea"
He got selected.

You know how and why did he say "TEA" when he knows very well that coffee was kept before.

(Answer: The question was "What is before you (U – alphabet) Reply was "TEA" ( T – alphabet) Alphabet "T" was before Alphabet "U"

Question5;
Interviewer said "I shall either ask you ten easy questions or one really difficult question. Think well before you make up your mind!"

The boy thought for a while and said, "my choice is one really difficult question."

"Well, good luck to you, you have made your own choice! Now tell me this. "What comes first, Day or Night?"

The boy was jolted into reality as his admission depends on the correctness of his answer, but he thought for a while and said, "It's the DAY sir!"

"How" the interviewer asked,

"Sorry sir, you promised me that you will not ask me a SECOND difficult question!"
___
Sometimes just thinking out of the box is all it takes!

Share with your friend and give them a special moment of thinking…!! 

IMPORTANT CURRENT AFFAIRS : 11.01.2014

1. Gorakhpur railway station entered Limca Book of Records as world’s longest rail platform in the world. The platform’s length is 1355.40 meters and was inaugurated in 6 October 2013.

2. National Bank for Agriculture and Rural Development (NABARD) has reduced refinance rate for banks and other lending agencies by 0.20 per cent to promote rural credit and rural infrastructure.

3. Delhi CM Arvind Kejriwal leaves 'janata darbar' midway after chaos. According to TV reports, Kejriwal left the meeting to hear public grievances midway after crowds swelled outside the Delhi Secretariat, with some even breaking the security barricades in order to have a word with the Delhi's CM.

4. Nandan Nilekani ready to contest LS polls on Congress ticket. 58-year - old Nilekani, who is the Chairman of the Unique Identification Authority of India, said talks are going on about the constituency from which he should be contesting. He added that he would join Congress, which has been very "supportive" to him and his ideas.

5. The Bank of Japan (BOJ) and RBI concluded an agreement that expands the maximum amount of the bilateral swap arrangement (BSA) between the two countries to USD 50 billion.

6. John Isner wins Auckland title in Open buildup. John Isner fired 23 aces in a match which had no service breaks to beat Lu Yen-hsun of Taiwan 7-6 (4), 7-6 (7) in the final.

7. Nirbheek, India's first gun for women, launched on 9 January 2014. It is a tribute to the gang rape victim of December 2012, Nirbhaya. Gun has been manufactured by the Indian Ordnance Factory, Kanpur.

8. Ali Larayedh, the Prime Minister of Tunisia resigned from his office on 9 January 2014, following a deal between the Islamist Ennahda Party of Ali Larayedh and the secular opposition for a transition to the democracy in the country. It’s likely that the name of Mehdi Jomaa will be announced by the President of Tunisia for appointment.

9. Rohan Bopanna and his Pakistani teammate Aisam-Ul-Haq Qureshi today ended as runners up in the Sydney International Tennis tournament.

10. India lose 0-2 against England in campaign opener at the Hero Hockey World League Final, in New Delhi on Friday.

11. Social activist IIa Pathak passed away due to prolonged illness in Ahmedabad on 9 January 2014. She was the founder of Ahmedabad Women’s Action Group (AWAG).

12. LIC has re-launched its “Jeevan Anand” plan, a participating non-linked plan which offers an attractive combination of protection and savings. 

Write a program to check whether the given string is a palindrome .

Palindrome is a string, which when read in both forward and backward way is same.
Example: radar, madam, pop, lol, rubber, etc.,

Program:
# include #include
int main(){
char string1[20 ];
int i, length;
int flag = 0;
printf("En ter a string: \n");
scanf("%s" , string1);
length = strlen(str ing1);
for(i=0;i<length ;i++){
if(string1 [i] != string1[le ngth-i-1]) {
flag = 1;
break;
}
}
if (flag){
printf("%s is not a palindrome \n", string1);
}
else{
printf("%s is a palindrome \n", string1);
}
return 0;
}
Output:
Enter a string: radar
"radar"is a palindrome
Explanatio n with example:
To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.
Consider a palindrome string:"radar",
---------- ---------- -------
index: 0 1 2 3 4
value: r a d a r
---------- ---------- -------
To compare it with the reverse of itself, the following logic is used:
0th character in the char array, string1 is same as 4th character in the same string.
1st character is same as 3rd character.
2nd character is same as 2nd character.
. . . .
ith character is same as'length-i- 1'th character.
If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome .
By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome .

Friday, January 10, 2014

Write a program to find the greatest of three numbers

Program:

# include int main(){
int a, b, c;
printf("En ter a,b,c: \n");
scanf("%d %d %d",&a,&b,&c);
if (a>b&&a>c){
printf("a is Greater than b and c");
}
else if (b>a&&b>c){
printf("b is Greater than a and c");
}
else if (c>a&&c>b){
printf("c is Greater than a and b");
}
else{
printf("al l are equal or any two values are equal");
}
return 0;
}
Output:
Enter a,b,c: 3 5 8
c is Greater than a and b
Explanatio n with examples:
Consider three numbers a=5,b=4,c= 8
if(a>b&&a>c) then a is greater than b and c
now check this condition for the three numbers 5,4,8 i.e.
if(5>4&&5>8) /* 5>4 is true but 5>8 fails */
so the control shifts to else if condition
else if(b>a&&b>c) then b is greater than a and c
now checking this condition for 5,4,8 i.e.
else if(4>5&&4>8) / * both the conditions fail */
now the control shifts to the next else if condition
else if(c>a&&c>b) then c is greater than a and b
now checking this condition for 5,4,8 i.e.
else if(8>5&&8>4) / * both conditions are satisfied */
Thus c is greater than a and b.

ग़ज़ल

rizw4nkh4n *ख़ुलूस-ए-दिल = purity of heart *एहतराम = आदर, सम्मान *मिज़ाजपुरसी = हाल-चाल पूछना *बाम = घर में सबसे ऊपर का कोठा और छत *इक़राम =...