I wrote the following code:
#include <stdio.h>
//To use fgets and printf I need to include stdio header
#include <string.h>
//To use strlen I need to include string header
#define MAXLINE 100 /* maximum input line length */
int suffix(char str[], char c);
/*
The function suffix prints all the substrings that
(1) starts with the letter stored in the variable 'c'
(2) included in the string str[]
Function should return the number of substrings.
*/
int main()
{
char user_input[MAXLINE]; /* current input line */
char ch = 0;
int counter = 0;
printf(" Please enter a string (up to 100 characters) and then press enter ");
/*
This will store a maximum of MAXLINE characters into the string from the standard input
and it will ensure it is null-terminated.
The fact that we've limited the input to the size of the array declared earlier
ensures that there's no possibility of buffer overruns.
*/
fgets(user_input, MAXLINE, stdin);
//Then let's clear the pesky newline that's been entered into the string using strlen:
user_input[strlen(user_input)-1] = '\0';
printf(" Please enter a character and then press enter ");
ch = getchar();
while ( getchar() != '\n' );
counter = suffix(user_input,ch);
return 0;
}
int suffix(char str[], char c);
{
int i=0,j=0;
for (i=0 ; (i < MAXLINE-1) ; i++)
{
if (str[i] == c)
for (j=i ; (j < MAXLINE-1) ; j++)
printf("%c", str[j]);
count++;
printf("\n");
}
return count;
}
I also wrote the following make file:
mystr: my_str.o
gcc -g -ansi -Wall my_str.o -o mystr
my_str.o: my_str.c
gcc -c -ansi -Wall my_str.c -o my_str.o
However, I get the following compilation errors:
VirtualBox:~/Desktop/Exercises/Assignments/my_str$ make
gcc -c -ansi -Wall my_str.c -o my_str.o
my_str.c:2:1: error: expected identifier or ‘(’ before ‘/’ token
my_str.c:5:1: error: expected identifier or ‘(’ before ‘/’ token
my_str.c: In function ‘main’:
my_str.c:38:5: error: expected expression before ‘/’ token
my_str.c:38:15: warning: character constant too long for its type [enabled by default]
my_str.c:45:5: warning: implicit declaration of function ‘suffix’ [-Wimplicit-function-declaration]
my_str.c:25:9: warning: variable ‘counter’ set but not used [-Wunused-but-set-variable]
my_str.c: At top level:
my_str.c:52:5: error: conflicting types for ‘suffix’
my_str.c:52:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
my_str.c:45:15: note: previous implicit declaration of ‘suffix’ was here
my_str.c:53:1: error: expected identifier or ‘(’ before ‘{’ token
make: *** [my_str.o] Error 1
VirtualBox:~/Desktop/Exercises/Assignments/my_str$
Please assist with "expected identifier or ‘(’ before ‘/’ token" error.
Why conflicting types of suffix? why implicit declaration (each function in C must be declared at the beginning of the program so I it`s not clear to me what is wrong here)?
Thanks
You are compiling with -ansi which according to man gcc disables C++ style comments (//):
-ansi In C mode, support all ISO C90 programs. In C++ mode, remove GNU extensions that conflict with ISO C++. This turns off certain features of GCC that are incompatible with ISO C90 (when compiling C code), or of standard C++ (when compiling C++ code), such as the "asm" and "typeof" keywords, and predefined macros such as "unix" and "vax" that identify the type of system you are using. It also enables the undesirable and rarely used ISO trigraph feature. For the C compiler, it disables recognition of C++ style // comments as well as the "inline" keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With