arrays - Converting strings to uppercase to compare with user input in C -
i attempting create program allow user search name in file. program successfully, occurred me not type in name capitalized in file. is, may search "sarah," name listed "sarah" in file name not found. around have attempted convert both strings upper case @ time of comparison. very, new teaching myself c, not if heading in right direction. @ point cannot program compile getting 2 errors "array initializer must initializer list or string literal." i'm assuming mean syntax not invalid in wrong direction. best way approach goal?
#include <stdio.h> #include <ctype.h> #include <string.h> int main(void) { file *infile; infile = fopen("workroster.txt", "r"); char rank[4], gname[20], bname[20], name[20]; printf("enter name: __"); scanf("%s", name); int found = 0; while(fscanf(infile, "%s %s %s", rank, bname, gname)== 3) { char uppername[40] = toupper(name[15]); char upperbname[40] = toupper(bname[15]); if(strcmp(uppberbname,uppername) == 0) { printf("%s number %s on roster\n", name, rank); found = 1; } } if ( !found ) printf("%s not on roster\n", name); return 0; }
use following function, included in strings.h
int strcasecmp(const char *s1, const char *s2);
in case change if statement
if(strcmp(uppberbname,uppername) == 0)
to
if(strcasecmp(bname,name) == 0)
and delete
char uppername[40] = toupper(name[15]); char upperbname[40] = toupper(bname[15]);
Comments
Post a Comment