- 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
Pages
The greatest mistake you can make in life is to be continually fearing you will make one
Wednesday, 19 January 2011
Daily 5 C programs-1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment