Part 1 Count the occurrences of a single character in a string. Example output: $ ./lab02 foobar o $ ./lab02 foobar invalid arguments Part 2 Count the occurrences of an N-character string in another string. Example output: $ ./lab02 abbabb bb 2 $ ./lab02 abbabb bc You may use the C library function strstr() if you wish. char *strstr(char *a, char *b) returns a pointer to the first occurrence of b if b occurs in a, or NULL if b does not occur in a

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section: Chapter Questions
Problem 7PP
icon
Related questions
Question

In C programming (not C++ or C#), how can I improve my code? I hided my name on the terminal.

 

#include <stdio.h>

#include <string.h>

int part1(count char str[],int c)

{

int i;

int count = 0;

 

for(i = 0; str[i] != '0'; i++)

if(str[i] == c)

count++;

return(count);

}

 

int part2(count char str[], int c)

{

int a;

int count = 0;

 

for(a = 0; str[a] != '0'; i++)

if(str[a] == c)

count++;

return(count);

}

 

int main(int argc, char **argv){

 

char *needle = argv[2];

 

if(strlen(needle) == 1);{

count = part1(c);

 

if(strlen(needle) == 1);{

count = part1(c);

 

else{

printf("invalid arguments");

 

}

 

 

printf(strrchr(strsep, char[c]));

 

return 0;

}

}

}

Lab5
Part 1
Count the occurrences of a single character in a string. Example output:
$ ./lab02 foobar o
2
$ ./lab02 foobar
invalid arguments
Part 2
Count the occurrences of an N-character string in another string. Example
output:
$ ./lab02 abbabb bb
2
$ ./lab02 abbabb bc
You may use the C library function strstr() if you wish. char
*strstr(char *a, char *b) returns a pointer to the first occurrence of b if
b occurs in a, or NULL if b does not occur in a
Transcribed Image Text:Lab5 Part 1 Count the occurrences of a single character in a string. Example output: $ ./lab02 foobar o 2 $ ./lab02 foobar invalid arguments Part 2 Count the occurrences of an N-character string in another string. Example output: $ ./lab02 abbabb bb 2 $ ./lab02 abbabb bc You may use the C library function strstr() if you wish. char *strstr(char *a, char *b) returns a pointer to the first occurrence of b if b occurs in a, or NULL if b does not occur in a
1$ micro lab02.c
@vlab01 lab02-
@vlab01 lab02-
@vlab01 lab02-|
lab02.c:3:11: error: unknown type name
int part1(count char str[],int c)
S$ gcc -g -o lab02 lab02.c
count'; did you mean
const'?
const
lab02.c:14:11: error: unknown type name 'count'; did you mean
int part2(count char str[], int c)
const'?
const
lab02.c: In function 'main':
lab02.c:30:3: error:
'count' undeclared (first use in this function)
count
part1(c);
lab02.c:30:3: note: each undeclared identifier is reported only once for each function it appears in
lab02.c:30:11: warning: implicit declaration of function 'part1' [-Wimplicit-function-declaration]
count =
part1(c);
lab02.c:30:17: error: 'c' undeclared (first use in this function)
count =
part1(c);
lab02.c:35:2: error:
'else' without a previous 'if'
else{
lab02.c:41:25: error: expected expression before 'char'
printf(strrchr(strsep, char[c]));
lab02.c:41:17: warning: passing argument 1 of 'strrchr' from incompatible pointer type [-Wincompatible-pointer-types]
printf(strrchr(strsep, char[c]));
In file included from lab02.c:2:
const char * restrict)'
/usr/include/string.h:253:14: note: expected 'const char *' but argument is of type 'char * (*)(char ** restrict,
extern char *strrchr (const char *.
__s, int __c)
Annn
Transcribed Image Text:1$ micro lab02.c @vlab01 lab02- @vlab01 lab02- @vlab01 lab02-| lab02.c:3:11: error: unknown type name int part1(count char str[],int c) S$ gcc -g -o lab02 lab02.c count'; did you mean const'? const lab02.c:14:11: error: unknown type name 'count'; did you mean int part2(count char str[], int c) const'? const lab02.c: In function 'main': lab02.c:30:3: error: 'count' undeclared (first use in this function) count part1(c); lab02.c:30:3: note: each undeclared identifier is reported only once for each function it appears in lab02.c:30:11: warning: implicit declaration of function 'part1' [-Wimplicit-function-declaration] count = part1(c); lab02.c:30:17: error: 'c' undeclared (first use in this function) count = part1(c); lab02.c:35:2: error: 'else' without a previous 'if' else{ lab02.c:41:25: error: expected expression before 'char' printf(strrchr(strsep, char[c])); lab02.c:41:17: warning: passing argument 1 of 'strrchr' from incompatible pointer type [-Wincompatible-pointer-types] printf(strrchr(strsep, char[c])); In file included from lab02.c:2: const char * restrict)' /usr/include/string.h:253:14: note: expected 'const char *' but argument is of type 'char * (*)(char ** restrict, extern char *strrchr (const char *. __s, int __c) Annn
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr