Write a function that takes a single string as its argument and returns a pointer to the first nonwhite character in the string
Answers
Explanation:
1 #include <stdio.h>
2
3
4 char* findChar(char *char_ptr);
5
6 int main() {
7
8 char *myString = " Hello";
9 char *myString2 = "";
10 char *myString3 = " \t \n \
11 ";
12
13
14 char *myString4 = "\t \n \
15 Kampar";
16
17 char *firstNonWhiteChar;
18
19 firstNonWhiteChar = findChar(myString4);
20
21 if( firstNonWhiteChar != NULL ) {
22 printf("\"%s\"\n", firstNonWhiteChar );
23 }
24
25 return 0;
26 }
27
28
29 char* findChar(char *char_ptr) {
30
31 char c;
32
33 if ( *char_ptr == '\0' ) {
34 printf("This is an empty string.\n");
35 return (NULL);
36 }
37
38 while( *char_ptr != '\0' ) {
39 c = *char_ptr;
40
41 if( c == ' ' || c == '\t' || c== '\n' )
42 ++char_ptr;
43 else
44 return char_ptr;
45
46 }
47
48 printf("There is no non-whitespace character in the string\n");
49 return (NULL);
50 }