While you can do the comparisons character-by-character, C provides char *strpbrk(const char *s, const char *accept)
that will provide the position in s
of the first byte (character) in accept
. Using "Tt"
as your accept
and word
as s
you receive a pointer to the first occurrence of 'T'
or 't'
in word
, e.g.
#include <stdio.h>
#include <string.h>
#define MAXWORD 51
int main (void) {
char word[MAXWORD], *ptr2t = NULL; /* storage and ptr to t or T */
size_t len = 0, pos = 0, mid = 0; /* word len, tT pos & mid pos */
fputs ("\nenter word (50 char max): ", stdout); /* prompt */
if (!fgets (word, MAXWORD, stdin)) { /* read up to 50 chars */
puts ("(user canceled input)");
return 0;
}
word[(len = strcspn (word, "\n"))] = 0; /* save len, trim \n from end */
if (!(ptr2t = strpbrk (word, "Tt"))) { /* locate 't' or 'T' in word */
puts ("-1");
return 0;
}
if (len == 1) { /* if length 1, no determination of half is possible */
puts ("(length is 1, half determination not possible)");
return 0;
}
mid = len / 2; /* get mid position */
pos = ptr2t - word; /* get postion in word */
#ifdef ODD_MID_IN_1ST /* if considering mid character in odd */
if (len & 1) { /* length part of 1st half, add 1 to */
mid += 1; /* mid. */
}
#endif
puts (pos < mid ? "1" : "2"); /* output 1 in 1st half, 2 otherwise. */
}
Following from my comments, you also have to handle the case where the length of the input is 1
as there can be no half determination (handle as you want), and you have to determine which half the mid-character in an odd length word belongs in. By default the mid-character belongs to the second-half. The define ODD_MID_IN_1ST
will change the behavior to place the mid-character in the first-half (up to you).
Compile
With gcc
you can use:
gcc -Wall -Wextra -pedantic -Wshadow -std=c11 -Ofast -o bin/tin1sthalf tin1sthalf.c
Where the options -Wall -Wextra -pedantic -Wshadow
enable full warnings (for the most part) and checks whether there are shadowed variables. -std=c11
specifies the language standard as C11 and -Ofast
is full optimization for gcc >= 4.6, prior to that the optimization levels were limited to O0
, O1
, O2
and O3
. Add -Werror
to treat warnings as errors (recommended). The executable output -o
will be placed in the bin/
directory (change as needed).
Always compile with full-warnings enabled (every compiler provides options to enable warnings) and do not accept code until it compiles without warning (-Werror
keeps you from cheating...)
Example Use/Output
Showing the various special case handling and general output of the program:
$ ./bin/tin1sthalf
enter word (50 char max): t
(lenght is 1, half determination not possible)
$ ./bin/tin1sthalf
enter word (50 char max): t_
1
$ ./bin/tin1sthalf
enter word (50 char max): _t
2
$ ./bin/tin1sthalf
enter word (50 char max): _t_
2
$ ./bin/tin1sthalf
enter word (50 char max): ___
-1
$ ./bin/tin1sthalf
enter word (50 char max): _T__
1
$ ./bin/tin1sthalf
enter word (50 char max): __t_
2
There are many, many ways to approach this problem. No one any more right than the other if they comply with the language standard or POSIX standard if applicable. The only difference is one of efficiency. As you learn to code, your focus should be on getting the code working and not worrying about micro optimizations early. Once you have your full program working, then profile it and worry about code optimizations at that point.
Let me know if you have questions.