- Write a program to print the number of characters entered?Count the Input characters
solution:
Output:#include<stdio.h> int main() { int x=0; while(getchar()!=EOF) ++x; printf("\nNumber of characters X=%d\n",x); return 0; }
hello world
Number of characters entered is X=11
- Write a program to count the number of words entered
Solution:
Output1:#include<stdio.h> int main() { int x=0,ch; while((ch=getchar())!=EOF ) { if(ch && x==0)//for the i/p xxxEOF x=1; if(ch=='\n' || ch==' ' || ch=='\t') ++x; } printf("\nNumber of words entered is X=%d\n",x); return 0; }
zccc
Number of words entered is X=1
Output2:
xcvvx cxcbsf
bbb
Number of words entered is X=3 - Write a program to print all input lines that are longer than 80 characters
- Write a program to remove all trailing blanks and tabs from each line of input and to delete entirely blank lines.
- Write a function reverse(s) that reverses the character string s
Pages
The greatest mistake you can make in life is to be continually fearing you will make one
Wednesday, 26 January 2011
Daily 5 C programs-2
Wednesday, 19 January 2011
EOF[End Of File]
- End of file[EOF] is a condition in computer operating system where no more data can be read from a data source(file or stream).
- EOF is actually a macro defined as an integer
with a negative value(normally -1).
- EOF is normally returned by functions that perform read operations to denote either an error or end of input. It is important to ensure you use an integer to store the return code from these functions, even if the function appears to be returning a char, such as
getchar()
orfgetc()
. - We cannot stop the program by directly giving -1. when you write '-1', you do not write the VALUE -1, but the character string { '-', '1' } your program will not stop, except if you type ctrl+D under linux, or ctrl+Z+enter under windows.
- Not a character
- A value that exists at the end of a file
- A value that could exist at the middle of a file
Example Program to find the EOF value:
/*************Program1***********/
#include<stdio.h>
int main()
{
int ch=getchar();
printf("%d",ch);
return;
}
---------------------------------------
/*************Program2***********/
#include<stdio.h>
int main()
{
printf("%d",EOF);
return;
}
In Windows if you enter the keys Control + Z you get EOF, in Linux it is Control + D
Output: -1
Daily 5 C programs-1
- Write a program to print the value of
EOF.?
Solution:/*************Program1***********/ #include<stdio.h> int main() { int ch=getchar(); printf("%d",ch); return; } --------------------------------------- /*************Program2***********/ #include<stdio.h> int main() { printf("%d",EOF); return; }
In Windows if you enter the keys Control + Z you get EOF, in Linux it is Control + D
Output: -1 - Write a program to read input from user and store it in a string until EOF(ctrl+d pressed in linux)
Solution1
Solution2#include<stdio.h> int main() { char str[100]; printf("Enter the string with maximum 100 characters\n"); scanf("%[^EOF]",str); printf("%s",str); return; }
#include<stdio.h> int main() { char buf[512]; int ch; int i=0,tab=0,line=0,blank=0; printf("Enter the line of text. Press ctrl+d to finish\n"); while((ch=getchar())!=EOF) { buf[i++]=ch; } printf("The entered string is %s\n",buf); return; }
Output:
Enter the string with maximum 100 characters
Hello world
Welcome to my blog
Hello world
Welcome to my blog
Write a Program to count blanks,tabs,newline?
Solution:
Output:/*Program to read input from user and store it in a string until EOF(ctrl+d pressed)*/ #include<stdio.h> int main() { char buf[512]; int ch; int i=0,tab=0,line=0,blank=0; printf("Enter the line of text. Press ctrl+d to finish\n"); while((ch=getchar())!=EOF) { buf[i++]=ch; if(ch=='\t') tab++; else if(ch=='\n') line++; else if(ch==' ') blank++; } printf("The entered string is %s\n",buf); printf("No of blanks=%d\n",blank); printf("No of tab=%d\n",tab); printf("No of Lines=%d\n",line); return; }
Enter the line of text.Press ctrl+d to finish
Hello world Welcome
How are you?
Did you Like my Blog
The entered string is Hello world Welcome
How are you?
Did you Like my Blog
No of blanks=7
No of tab=2
No of Lines=3
- Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank?
Solution:
Output:#include<stdio.h> int main() { char txt[100],out[100],ch; int i=0,j=0; printf("Enter the string\n"); scanf("%[^EOF]",txt); while(txt[i+1]!=EOF) { ch=txt[i]; if(ch!=' ' || (ch==' ' && txt[i+1]!=' ')) out[j++]=ch; i++; } printf("\nOutput String=%s\n",out); return; }
./a.out
Enter the string
a s d a
Output String=a s d a
- Write a program to copy its input to its output, replacing each tab by
\t
, each backspace by\b
, and each backslash by\\
. This makes tabs and backspaces visible in an unambiguous way.
Solution:
Output:#include<stdio.h> int main() { char buf[512]; int ch,i=0; printf("Enter the line of text. Press ctrl+d to finish\n"); while((ch=getchar())!=EOF) { if(ch=='\t' || ch=='\n' || ch==' ') buf[i++]='\\';/*to print \*/ if(ch=='\t') buf[i++]='t'; else if(ch=='\n') buf[i++]='n'; else if(ch==' ') buf[i++]='b'; else/*for all other char*/ buf[i++]=ch; } buf[i++]='\0'; printf("The entered string is %s\n",buf); return; }
Enter the line of text.Press ctrl+d to finish
Hello World Welcome
C Programming
The entered string is Hello\bWorld\t\tWelcome\b\nC\b\bProgramming\n
- Write a program that prints its input one word per line?
Solution1:
Output:#include<stdio.h> int main() { char txt[100],out[100],ch; int i=0,j=0; printf("Enter the string\n"); scanf("%[^EOF]",txt); while(txt[i]!=EOF) { ch=txt[i]; if(ch==' ') out[j++]='\n'; else out[j++]=ch; i++; } printf("\nOutput String=%s\n",out); return; }
Enter the string
hello World Welcome
Output String=hello
World
Welcome
Tuesday, 18 January 2011
Some questions about Arrays with answers
- How will you define an array?
Storage class datatype array_name[size of array] - static int sum[8];
- auto float fun[20]; (or) float fun[20];
- Is it necessary to specify the storage class in array definition? what is the default storage class for arrays?
Answer:
Storage class is optional. If the array is defined with in a function or a block the default storage class is auto. If the array is defined outside of function then the default storage class is extern. - If the array elements are not initialized then what is the default initial value?for example: int x[5]; what is the value of x[1]?
Answer:
If the array is not initialized then the array has garbage value. If the array is a external or global array then the default initial value is 0
Example:
#include<stdio.h>
int main()
{
int a[5];
printf("%d",a[2]);
return;
}
Output:
./a.out 195392
#include<stdio.h> int e[3]; int main() { int i,a[3]; for(i=0;i<3;i++) { printf("a[%d]=%d\t",i,a[i]); printf("e[%d]=%d\n",i,e[i]); } return; } output a[0]=-1530409104 e[0]=0 a[1]=32767 e[1]=0 a[2]=0 e[2]=0
- Consider we have a 10 element array what happen if we try to access the 12th element?
There is no bound checking in array. So if we try to access the array element out of its size then it will return garbage value.
#include<stdio.h> int e[10]; int main() { int i,a[10]; for(i=10;i<15;i++) { printf("a[%d]=%d\t",i,a[i]); printf("e[%d]=%d\n",i,e[i]); } return; } output ./a.out a[10]=0 e[10]=0 a[11]=11 e[11]=0 a[12]=0 e[12]=0 a[13]=0 e[13]=0 a[14]=366693453 e[14]=0
- Can I declare the array without specifying the size? int a[]; is it allowed?
Answer:
No its not allowed.
error: array size missing in ‘a’ - int a[]={1,2,3,4,5}; Is it allowed?
Answer:Yes. The number of array element is optional when initial values are present
- Consider int a[5]={10,20,30,40,50}; printf("a=%d",*a); what is the output?
#include<stdio.h> int main() { int a[5]={10,20,30,40,50}; printf("a=%d",*a); return; }
Answer:
a=10 - Consider int a[5]={10,20,30,40,50}; printf("a=%d",a); what is the output?
We cannot print the entire array elements using only the array name#include<stdio.h>
int main()
{
int a[5]={10,20,30,40,50};
printf("a=%d",a);
return;
}
Answer:
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
sunitha@sunitha-laptop:~/cpgm$ ./a.out
a=425449152 - The last character in a array must be a NULL(\0) character what happen if I missed the '\0' character? char vowels[6]={'a','e','i','o','u'}; Is it correct?
int main()
{
char vowels[6]={ 'a','e','i','o','u'};
printf("char=%c",vowels[5]);
return;
}
Output
./a.out
char= - What happen If I am not giving space for '\0' character? char flag[4]={'T','R','U','E'}; is it correct?
Answer
This type of declaration is incorrect but the compiler wont show any error. but flag[5]={'T','R','U','E'}; is the correct form (or) flag[]={'T','R','U','E'}; - What happen If I am not giving space for '\0' character in string? char str[5]="Welco"; is it correct?
Answer
Some unwanted character will be printed at the end of the string.
#include<stdio.h>
int main()
{
char str[5]="Welco";
printf("string=%s to india",str); return;
}
output
./a.out
string=Welco to india - x is a array pointer. what is the value for *x++?
#include<stdio.h>
int main()
{ int x[5]={10,20,30,40,50};
printf("ans=%d",*x++);
return;
}
output
error: lvalue required as increment operand - Is there any way to find the size of the array?
We can find the size of the array using sizeof operator.#include<stdio.h>int main() { int x[15]; printf("ans=%d",sizeof(x)); return; } Output: ./a.out ans=60
#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%d",sizeof(x)); return; } Output: ./a.out ans=6
- Can I print the characters given in single quotes as string?
#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%s",x); return; } Output: ./a.out ans=aeiou
- What is the output for the following program?
#include<stdio.h> int main() { char x[]={'a','e','i','o','u'}; printf("ans=%s",x); return; }
Answer:
ans=aeiou
Some unwanted character is printed at the end of the string - What is the output for the following program?
./a.out#includeint main(){ int x[5]={10,20,30,40,50};printf("ans=%d",(*x)++);return;} Output:
ans=10 - What is the output for the following program?
#includeint main(){int x[5]={10,20,30,40,50};printf("ans=%d",++*x);return;} Output./a.outans=11 - Do we have any minimum or maximum size restrictions in array index?
- What is the maximum index(sixe) number an array can have?
The maximum size of an indexed array is 232 - 1 or 4,294,967,295. An attempt to create an array that is larger than the maximum size results in a run-time error. - Why array index is starting from zero? (or) Why arrays not store values from 1?
a[n] has to implemented internally as *(a+n)so it is easy if it starts from 0; otherwise it has to be *(a+n-1) - Can I declare an array with 0 size? int a[0]; is it allowed?
#include<stdio.h> int main() { int x[0]; *x=20; printf("ans=%d",x[0]); return; } Output ./a.out ans=20
- Can I declare an array with negative size? int a[-5]; is it allowed?
#include<stdio.h> int main() { int x[-5]; *x=20; printf("ans=%d",x[0]); return; } Output compilation error: size of array ‘x’ is negative
- Can I compare 2 arrays with single operator?
Single operations which involve entire arrays are not permitted in c. Thus if a and b are similar arrays (ie same datatype, same dimension and same size) assignment operations, comparison operations must be carried out on element by element basis. - Can I have comma(,) alone in the array list? int x[5]={,,,,}; Is it allowed?
error: expected expression before ‘,’ token - What happen if the array size is less than the number of initialized values?int a[5]={1,2,3,4,5,6,7,8,9,}; is it allowed?
Answer
It will give warning as "excess elements in array initializer" but it will produce the correct output for the array size and the remaining elements are garbage value. So the total output is incorrect#include<stdio.h> int main() { int i; char x[3]={'a','e','i','o','u','\0'}; printf("String=%s\n",x); return; } Output ./a.out String=aeiQ�
#include<stdio.h> int main() { int i,a[3]={1,2,3,4,5,6,7,8,9}; for(i=0;i<9;i++) printf("a[%d]=%d\n",i,a[i]); return; } output ./a.out a[0]=1 a[1]=2 a[2]=3 a[3]=3 a[4]=0 a[5]=0 a[6]=-1332024243 a[7]=32532 a[8]=0
- How to pass and access the array values to the function using call by value method?
- How to pass and access the array values to the function using call by reference method?
- Array name is a pointer. If an array is passed to a function and several of its elements are altered with in the function, are these changes recognized in the calling portion of the program?
Answer:If the array element is altered within the function, the alteration will be recognized in the calling portion of the program.
output#include<stdio.h> void fun(char x[],int ); int main() { char x[]={'a','e','i','o','u','\0'}; printf("before=%s\n",x); fun(x,5); printf("after=%s",x); return; } void fun(char x[],int n) { int i; for(i=0;i<n;i++) { x[i]=x[i]+i; } }
./a.out
before=aeiou
after=afkry
- Can an array can be passed from a function to the calling portion of the program via return statement?
Subscribe to:
Posts (Atom)