#include <stdio.h> #include <string.h> int check_vowel(char); int main() { char s[100], t[100]; int i, j = 0; printf("Enter a string to delete vowels\n"); gets(s); for(i = 0; s[i] != '\0'; i++) { if(check_vowel(s[i]) == 0) { //not a vowel t[j] = s[i]; j++; } } t[j] = '\0'; strcpy(s, t); //We are changing initial string printf("String after deleting vowels: %s\n", s); return 0; } int check_vowel(char c) { switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return 1; default: return 0; } }
Friday, October 11, 2013
remove vowels string c
Thursday, October 10, 2013
Searching programs in c
Linear Search
#include <stdio.h> int main() { int array[100], search, c, number; printf("Enter the number of elements in array\n"); scanf("%d",&number); printf("Enter %d numbers\n", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to search\n"); scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) { if ( array[c] == search ) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.\n", search); return 0; }
Binary Search
#include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); return 0; }
Tuesday, September 10, 2013
Programming Problems(Find the output??)
What is the output of following programe?
Ques1.
#include <stdio.h>
main( )
{
i n t a, b y c;
scanf ( "%3d %3d %3d" , &a, &b, &c) ;
. . . .
}
Input :
1234 5678 9
Ans. Assignment will be like this:
a=123 b=4 c=567
Ques2.
Wednesday, September 4, 2013
Some interesting programs
#1 Printing the string containing Blank spaces while using Scanf
Problem: While using scanf for getting a string we have the limitation of string upto blank space.As soon as blank spaces occurs in a string scanf terminates to take that input.
i.e .
char line[80];
char line[80];
scanf("%s",line);
for above program if we type
India is my country.
then line[ ] be assigned only "India"
Solution:
To solve this problem we could use a conversion process.
Actually the "%s " in scanf converts our input to some string terminated with \0. As soon as the \0 occurs it does not take input. So following the rule of type conversion we can instruct our compiler to take input according to our own way.
An alternative method to print a string containing white space chararcter and uppercase character is that , write all uppercase letters including white sapce should be added in a square bracket in place of s, So that our compiler could interpret them and read them.
scanf (" %[ ABCDEFGHIJKLMNOPQRSTUWXYZ]", line ) ;
Now if our input is
INDIA IS MY COUNTRY.
then line will be assigned the whole string "INDIA IS MY COUNTRY"
but now since we have not added the lowercase character hence as soon as it recognizes the lowercase letter it will terminate the scanning process.
Obviously , we could add lowercase character as well, but it will become cumbersome for us to write all those we want.
Now seeing the above solution, it comes to our mind that the above solution could be better if we could make compiler interpret the above things in opposite manner.i.e. what I mean to say that there should be some instruction , applying which we could help to ignore those characters which we are writing in code.
Because the ignored characters will be lesser than the used ones.
So ans is yes , we could use a circumflex (^)before our letters to be interpreted in opposite manner.
thus for ignoroing \n
the code will be simply like this :
scanf("%[^\n]",line);
Some C Facts
Scanf : In scanf all the operators are preceeded with the % sign because in scanf the argument presented by it are actually address. But the same does not apply to an array or string,why?
Because an array or string name is already an address of its first location , so no need to use % sign.
main( )
{char item[20];
int partno;
float cost;
scanf(" %s %d %f', item, &partno, &cost);
}
Because an array or string name is already an address of its first location , so no need to use % sign.
main( )
{char item[20];
int partno;
float cost;
scanf(" %s %d %f', item, &partno, &cost);
}
Sunday, September 1, 2013
Operator Precedence Groups
Operator category Operators Associativity
unary operators - ++ ! sizeof (type) Right to Left
arithmetic multiply, divide and remainder * I % Left to Right
arithmetic add and subtract + - Left to Right
relational operators < <= > >= Left to Right
equality operators != Left to Right
unary operators - ++ ! sizeof (type) Right to Left
arithmetic multiply, divide and remainder * I % Left to Right
arithmetic add and subtract + - Left to Right
relational operators < <= > >= Left to Right
equality operators != Left to Right
logical and && Left to Right
logical or || Left to Right
conditional operator ? : Right to Left
assignment operators = += -= *= /= %= Right to Left
conditional operator ? : Right to Left
assignment operators = += -= *= /= %= Right to Left
Modulus Operator (%)
How to find remainders sign when one of the operand is negative??
When performing division it is very easy to find sign of quotient on the basis of elementary mathematics that if the two operands are of same sign then quotientwill be positive but negative in other case.
Now it feels cumbersome to us when we have to find the sign of remainder.It seems to us that the 'C' language is violating the rule of mathematics, But it is not actually true.
The mathematics is correct behind this , while our concepts have proven to be weak.
In all opeartions of modiulus following ciondition should be satisfied:
a = ( ( a / b) * b) + (a % b)
i.e.
Dividend = Quotient * Divisor+ Remainder
From above equation
Remainder = Dividend - Quotient * Divisor
following the above equation we can calculate the remainder for following cases :
Finding remainder with negative opearnds
Dividend||Divisor||Quoteint||Remainder
10 3 3 1 // Remainder = 10- 3*3
-10 3 -3 -1 //Remainder = 10- {(-3)*3}
10 -3 -3 1 //Remainder = 10- {(-3)*(-3)}
-10 -3 3 -1 //Remainder = -10- {3*(-3)}
When performing division it is very easy to find sign of quotient on the basis of elementary mathematics that if the two operands are of same sign then quotientwill be positive but negative in other case.
Now it feels cumbersome to us when we have to find the sign of remainder.It seems to us that the 'C' language is violating the rule of mathematics, But it is not actually true.
The mathematics is correct behind this , while our concepts have proven to be weak.
In all opeartions of modiulus following ciondition should be satisfied:
a = ( ( a / b) * b) + (a % b)
i.e.
Dividend = Quotient * Divisor+ Remainder
From above equation
Remainder = Dividend - Quotient * Divisor
following the above equation we can calculate the remainder for following cases :
Finding remainder with negative opearnds
Dividend||Divisor||Quoteint||Remainder
10 3 3 1 // Remainder = 10- 3*3
-10 3 -3 -1 //Remainder = 10- {(-3)*3}
10 -3 -3 1 //Remainder = 10- {(-3)*(-3)}
-10 -3 3 -1 //Remainder = -10- {3*(-3)}
Division operator in c (Carefully divide)
Division Operator(/)
Integer division:
If two operands performing division operation are integer then the result will be an integer iwith truncated decimal values.
e.g.
a=10; b=3;
then a/b = 3 ; (rather than 3.333 in normal algebra.)
Ques. What will be the output of the following programme?
#include<stdio.h>
int main()
{
float c;
printf("%d\n", 10/3);
printf("%f\n", 10/3);
c=10/3;
printf("%f\n",c);
return 0;
}
output:
3
0.000000
3.000000
Notice that all three expressions has both integers but all three expressions are having different outputs. all three expressions are performing integer division hence output first will be integer.
That is 10/3 =3 in short.
If one of the operands in division operation are float then result will be float.
#include<stdio.h>
int main()
{
float c; printf("%d\n", 10.0/3);
printf("%f\n", 10.0/3);
c=10.0/3;
printf("%f\n",c);
printf("%d\n", 10/3.0);
printf("%f\n", 10/3.0);
c=10/3.0;
printf("%f\n",c);
printf("%d\n", 10.0/3.0);
printf("%f\n", 10.0/3.0);
c=10.0/3.0;
printf("%f\n",c);
return 0;
}
Ans.
-1431655765
3.333333
3.333333
-1431655765
3.333333
3.333333
-1431655765
3.333333
3.333333
Integer division:
If two operands performing division operation are integer then the result will be an integer iwith truncated decimal values.
e.g.
a=10; b=3;
then a/b = 3 ; (rather than 3.333 in normal algebra.)
Ques. What will be the output of the following programme?
#include<stdio.h>
int main()
{
float c;
printf("%d\n", 10/3);
printf("%f\n", 10/3);
c=10/3;
printf("%f\n",c);
return 0;
}
output:
3
0.000000
3.000000
Notice that all three expressions has both integers but all three expressions are having different outputs. all three expressions are performing integer division hence output first will be integer.
That is 10/3 =3 in short.
If one of the operands in division operation are float then result will be float.
#include<stdio.h>
int main()
{
float c; printf("%d\n", 10.0/3);
printf("%f\n", 10.0/3);
c=10.0/3;
printf("%f\n",c);
printf("%d\n", 10/3.0);
printf("%f\n", 10/3.0);
c=10/3.0;
printf("%f\n",c);
printf("%d\n", 10.0/3.0);
printf("%f\n", 10.0/3.0);
c=10.0/3.0;
printf("%f\n",c);
return 0;
}
Ans.
-1431655765
3.333333
3.333333
-1431655765
3.333333
3.333333
-1431655765
3.333333
3.333333
C-Programming questions part-1(basic)
1.How many characters are in the following string constant?
" \ t To continue, press the \"RETURN\" key\n"
Ans: total 38
27 normal characters
five blank spaces,
one special character(,)
four escape sequences (horizontal tab, two quotation marks and newline)
and the null character (\ 0) at the end of the string.
2.Which of the following are not valid identifires?
4th , _radius, value, the_tax_return,"My_name", area of circle
Ans. 4th,"My_name",area of circle
3.What is the differences between string constant "A"and character constant 'A'.
Ans. a character constant has an equivalent integer value, whereas a single-character string constant does not have an equivalent integer value and, in fact, consists of two characters -the specified character followed by the null character ( \ 0).
4.What will be the output of following programme?
char array[11] = "Hello World";
printf("%s\n",array);
Ans. Error
5. The unary operator always preceeds their operands (Trur/False)
Ans. Flase
i++,i-- are examples where unary operators comes after the operands
6. Write the unary operators.
Ans. ++,--,(type),sizeof,-,!
5. The unary operator always preceeds their operands (Trur/False)
Ans. Flase
i++,i-- are examples where unary operators comes after the operands
6. Write the unary operators.
Ans. ++,--,(type),sizeof,-,!
7. While considering the operators precedence and associativity , what is the difference between precedence and associativity?
Ans. Precedence tells the priority order of the operators in any expression.but ambuiguity arises while having same prirority opereators occur in same expression.In that case an order from either right to left or left to right is followed to resolve this problem.
For example :
in a=3+4-5
+ and - are of same priority.
then according to their associativity(Left to right) their operation will be performed in that order. Hence first addition is performed then subtraction.
NOTE: In all the operators only unary operators ,conditional operators and assignment operators are having associativity from right to left.
8.What is the value of following expression?
i = 2 * 5 / 2
Ans. Since * and / are having same priority then according to their associativity left to right * operation is performed first.
hence i=2*5/2=10/2=5
if expression would be like this i = 2 * (5/2) then answer will be i=2*2=4
9. In what general category do the #define and #include statements fall?
Ans. Preprocessor Statement
8.What is the value of following expression?
i = 2 * 5 / 2
Ans. Since * and / are having same priority then according to their associativity left to right * operation is performed first.
hence i=2*5/2=10/2=5
if expression would be like this i = 2 * (5/2) then answer will be i=2*2=4
9. In what general category do the #define and #include statements fall?
Ans. Preprocessor Statement
Subscribe to:
Posts (Atom)