Advertisement
Guest User

Simple programs

a guest
Mar 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. 1. Write a program that print word in new line when get white space.
  2. #include <stdio.h>
  3.  
  4. int main(){
  5.     char s[101];
  6.     while(scanf(" %s", s)!=EOF && s[0]!='#'){ /// if input contains #, it will break the loop. Remove if needed
  7.         printf("%s\n", s);
  8.     }
  9.     return 0;
  10. }
  11.  
  12. 2. Write a program that print word in new line after removing extra space.
  13. /// duplicate
  14.  
  15. 3. Write a program that will count the number of white space in the input string.
  16. #include <stdio.h>
  17.  
  18. int cntSpace(char *s){
  19.     int cnt=0, i;
  20.     for(i=0; s[i]!='\0'; i++){
  21.         if(s[i]==' ') cnt++;
  22.     }
  23.     return cnt;
  24. }
  25.  
  26. int main(){
  27.     char s[1001];
  28.     scanf("%[^\n]", s); /// you can use gets(s) as well which is deprecated from standard
  29.     printf("%d\n", cntSpace(s));
  30.     return 0;
  31. }
  32.  
  33. 4. Write a program to find ascii code of a character
  34. #include <stdio.h>
  35.  
  36. int main(){
  37.     char c;
  38.     scanf("%c", &c);
  39.     printf("%d\n", c);
  40.     return 0;
  41. }
  42.  
  43. 5. Write a program to copy string without strcpy.
  44. #include <stdio.h>
  45. #include <string.h>
  46.  
  47. void copy(char *b, char *a, int len){
  48.     int i;
  49.     for(i=0; i<len; i++){
  50.         b[i]=a[i];
  51.     }
  52.     b[i]='\0';
  53. }
  54.  
  55. int main(){
  56.     int sz=101; /// maximum string size, change it as required
  57.     char ar[sz], br[sz];
  58.     scanf("%s",  ar); /// use gets(ar) [non-standard way] or scanf("%[^\n]", s) to scan a whole line
  59.     int len=strlen(ar);
  60.     copy(br, ar, len);
  61.     printf("%s\n", br);
  62.     return 0;
  63. }
  64.  
  65. 6. Write a program to check input is an alphabet or number or special character.
  66. #include <stdio.h>
  67.  
  68. bool isAlpha(char ch){
  69.     if(ch > 'Z') ch = ch - 'a' + 'A';
  70.     return (ch >= 'A' && ch <= 'Z');
  71. }
  72.  
  73. bool isDigit(char ch){
  74.     return ( ch >= '0' && ch <= '9' );
  75. }
  76.  
  77. int main(){
  78.     char c;
  79.     scanf("%c", &c);
  80.     if(isDigit(c)) printf("Digit\n");
  81.     else if(isAlpha(c)) printf("Alphabet\n");
  82.     else printf("Special character\n");
  83.     return 0;
  84. }
  85.  
  86. 7. Write a program to find length of string and number of white space in the input string.
  87. #include <stdio.h>
  88.  
  89. int cntSpace(char *s){
  90.     int cnt=0, i;
  91.     for(i=0; s[i]!='\0'; i++){
  92.         if(s[i]==' ') cnt++;
  93.     }
  94.     return cnt;
  95. }
  96.  
  97. int strLen(char *s){
  98.     int cnt=0, i;
  99.     for(i=0; s[i]!='\0'; i++) cnt++;
  100.     return cnt;
  101. }
  102.  
  103. int main(){
  104.     char s[101];
  105.     scanf("%[^\n]", s); /// you can use gets(s) as well which is deprecated from standard
  106.     printf("String length= %d\n", strLen(s));
  107.     printf("Number of whitespaces= %d\n", cntSpace(s));
  108.     return 0;
  109. }
  110.  
  111. 8. Write a program to make a simple calculator that takes only number as input.
  112. /// need to be more specific, is it just an integer input output or something else?
  113.  
  114. 9. Write a program that will count number of vowel, consonant and digit and whitespace.
  115. #include <stdio.h>
  116.  
  117. bool isVowel(char c){
  118.     if(c>'Z') c=c-'a'+'A';
  119.     if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U') return 1;
  120.     return 0;
  121. }
  122.  
  123. bool isCons(char c){
  124.     if(c>'Z') c=c-'a'+'A';
  125.     if(c>='A' && c<='Z') return 1;
  126.     return 0;
  127. }
  128.  
  129. bool isDig(char c){
  130.     return ( c>='0' && c<='9' );
  131. }
  132.  
  133. int main(){
  134.     int i=0, v=0, c=0, d=0, sp=0;
  135.     char s[101];
  136.     scanf("%[^\n]", s); /// you can use gets(s) as well which is deprecated from standard
  137.     for(i=0; s[i]1='\0'; i++){
  138.         if(isDig(s[i])) d++;
  139.         else if(isVowel(s[i])) v++;
  140.         else if(isCons(s[i])) c++;
  141.         else if(s[i]==' ') sp++;
  142.     }
  143.     printf("Vowels= %d, Consonants= %d, Digits= %d, Whitespaces= %d\n", v, c, d, sp);
  144.     return 0;
  145. }
  146.  
  147. 10. Write a program that will take a string as input and print whether this is a comment or not.
  148. /// What's the definition of comment?
  149.  
  150. 11. Write a program that will take a line as input and print the line with removing extra space.
  151. #include <stdio.h>
  152.  
  153. int main(){
  154.     char s[101];
  155.     while(scanf("%s", s)!=EOF && s[0]!='#'){ ///used # as ending line, change/remove as required
  156.         printf("%s", s);
  157.     }
  158.     return 0;
  159. }
  160.  
  161. 12. Write a program for symbol table.
  162. /// what's the definition of symbol table?
  163.  
  164. 13. Write a program for identify token (ex: find operator)
  165. /// need more detail about it
  166.  
  167. 14. Write a program to find the first & follow of grammar.
  168. /// need more detail, it's totally unclear to me
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement