0% found this document useful (0 votes)
17 views1 page

Pointers Programtask Slide88

The document defines a function findFirst that searches a string for a given character and returns a pointer to the first occurrence or NULL if not found. It also contains a main function that demonstrates calling findFirst to search a user-entered string for a character and print the result.

Uploaded by

petra710386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Pointers Programtask Slide88

The document defines a function findFirst that searches a string for a given character and returns a pointer to the first occurrence or NULL if not found. It also contains a main function that demonstrates calling findFirst to search a user-entered string for a character and print the result.

Uploaded by

petra710386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#define MAXCHAR 50

char *findFirst(char *p, char a) {


char *res;
for(int i = 0; i < MAXCHAR; i++) {
while(*(p + i) != '\0') {
if(*(p + i) == a) {
res = p;
} else {
res = NULL;
}
}
}
return res;
}

int main() {
char text[MAXCHAR];
char a, *p;

printf("Enter text > ");


fgets(text, MAXCHAR, stdin);

printf("Enter character > ");


scanf("%c", &a);

p = findFirst(&text[0], a);
if(p == NULL) {
printf("Character %c is not found\n", a);
} else {
printf("%s\n", p);
}

return 0;
}

You might also like