Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include <stdio.h>
  2. //To use fgets and printf I need to include stdio header
  3.  
  4. #include <string.h>
  5. //To use strlen I need to include string header
  6.  
  7. #define MAXLINE 100 /* maximum input line length */
  8.  
  9. int suffix(char str[], char c);
  10.  
  11. /*
  12. The function suffix prints all the substrings that
  13. (1) starts with the letter stored in the variable 'c'
  14. (2) included in the string str[]
  15.  
  16. Function should return the number of substrings.
  17.  
  18. */
  19.  
  20.  
  21. int main()
  22. {
  23. char user_input[MAXLINE]; /* current input line */
  24. char ch = 0;
  25. int counter = 0;
  26.  
  27.  
  28. printf(" Please enter a string (up to 100 characters) and then press enter ");
  29.  
  30. /*
  31. This will store a maximum of MAXLINE characters into the string from the standard input
  32. and it will ensure it is null-terminated.
  33. The fact that we've limited the input to the size of the array declared earlier
  34. ensures that there's no possibility of buffer overruns.
  35. */
  36. fgets(user_input, MAXLINE, stdin);
  37.  
  38. //Then let's clear the pesky newline that's been entered into the string using strlen:
  39. user_input[strlen(user_input)-1] = '';
  40.  
  41. printf(" Please enter a character and then press enter ");
  42. ch = getchar();
  43. while ( getchar() != 'n' );
  44.  
  45. counter = suffix(user_input,ch);
  46.  
  47. return 0;
  48. }
  49.  
  50.  
  51.  
  52. int suffix(char str[], char c);
  53. {
  54. int i=0,j=0;
  55.  
  56. for (i=0 ; (i < MAXLINE-1) ; i++)
  57. {
  58. if (str[i] == c)
  59. for (j=i ; (j < MAXLINE-1) ; j++)
  60. printf("%c", str[j]);
  61. count++;
  62. printf("n");
  63. }
  64.  
  65. return count;
  66.  
  67. }
  68.  
  69. mystr: my_str.o
  70. gcc -g -ansi -Wall my_str.o -o mystr
  71.  
  72. my_str.o: my_str.c
  73. gcc -c -ansi -Wall my_str.c -o my_str.o
  74.  
  75. VirtualBox:~/Desktop/Exercises/Assignments/my_str$ make
  76. gcc -c -ansi -Wall my_str.c -o my_str.o
  77. my_str.c:2:1: error: expected identifier or ‘(’ before ‘/’ token
  78. my_str.c:5:1: error: expected identifier or ‘(’ before ‘/’ token
  79. my_str.c: In function ‘main’:
  80. my_str.c:38:5: error: expected expression before ‘/’ token
  81. my_str.c:38:15: warning: character constant too long for its type [enabled by default]
  82. my_str.c:45:5: warning: implicit declaration of function ‘suffix’ [-Wimplicit-function-declaration]
  83. my_str.c:25:9: warning: variable ‘counter’ set but not used [-Wunused-but-set-variable]
  84. my_str.c: At top level:
  85. my_str.c:52:5: error: conflicting types for ‘suffix’
  86. my_str.c:52:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
  87. my_str.c:45:15: note: previous implicit declaration of ‘suffix’ was here
  88. my_str.c:53:1: error: expected identifier or ‘(’ before ‘{’ token
  89. make: *** [my_str.o] Error 1
  90. VirtualBox:~/Desktop/Exercises/Assignments/my_str$
  91.  
  92. -ansi
  93. In C mode, support all ISO C90 programs. In C++ mode, remove GNU
  94. extensions that conflict with ISO C++.
  95.  
  96. This turns off certain features of GCC that are incompatible with
  97. ISO C90 (when compiling C code), or of standard C++ (when compiling
  98. C++ code), such as the "asm" and "typeof" keywords, and predefined
  99. macros such as "unix" and "vax" that identify the type of system
  100. you are using. It also enables the undesirable and rarely used ISO
  101. trigraph feature. For the C compiler, it disables recognition of
  102. C++ style // comments as well as the "inline" keyword.
  103.  
  104. int suffix(char str[], char c)
  105. {
  106. ...
  107. }
  108.  
  109. int i=0,j=0;
  110. int count = 0;
  111.  
  112. #include <stdio.h>
  113.  
  114. /*To use fgets and printf I need to include stdio header*/
  115.  
  116. #include <string.h>
  117. /*To use strlen I need to include string header*/
  118.  
  119. #define MAXLINE 100 /* maximum input line length */
  120.  
  121. int suffix(char str[], char c);
  122.  
  123. /*
  124. The function suffix prints all the substrings that
  125. (1) starts with the letter stored in the variable 'c'
  126. (2) included in the string str[]
  127. (3) returns the number of substrings
  128. */
  129.  
  130.  
  131. int main()
  132. {
  133. char user_input[MAXLINE]; /* current input line */
  134. char ch = 0;
  135. int counter = 0;
  136. int i = 0;
  137.  
  138. for (i=0; i < MAXLINE-1 ; ++i)
  139. user_input[i] = 0;
  140.  
  141.  
  142. printf(" nPlease enter a string (up to 100 characters) and then press entern ");
  143.  
  144.  
  145. for (i=0; i < MAXLINE-1 && ( ch=getchar())!= EOF && ch !='n'; ++i)
  146. user_input[i] = ch;
  147.  
  148. user_input[i] ='';
  149.  
  150. printf(" nPlease enter a character and then press entern");
  151. scanf(" %c", &ch);
  152.  
  153. counter = suffix(user_input,ch);
  154.  
  155. printf("substring counter is = %d ", counter);
  156.  
  157. printf("n");
  158.  
  159. return 0;
  160. }
  161.  
  162.  
  163.  
  164. int suffix(char str[], char c)
  165. {
  166. int i=0,j=0;
  167. int count = 0;
  168.  
  169. printf("n########################nn");
  170. printf("Substrings are as follows: n");
  171.  
  172. for (i=0 ; (i < MAXLINE-1) ; i++)
  173. {
  174. if (str[i] == c)
  175. {
  176. for (j=i ; (j < MAXLINE-1) ; j++)
  177. printf("%c", str[j]);
  178.  
  179. count++;
  180. printf("n");
  181. }
  182. }
  183.  
  184. if (count == 0)
  185. printf("There are no substrings which begin with the specified charactern");
  186.  
  187. return count;
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement