c - Coverting an unknown length string to lower case issues -



c - Coverting an unknown length string to lower case issues -

so i'm not c i'm designing glut application reads in file not case sensitive. create easier want convert strings lower case. made function makelower modifying variable passed in reference.

i have while loop in method makelower seems through part of first iteration of while loop , exe crashes. tips great, thanks!

output:

c:\users\mark\documents\visual studio 2010\projects\project 1\debug>"project 1.e xe" ez.txt line #draw diamond ring character #

then error "project 1.exe has stopped working"

code:

void makelower(char *input[]){ int = 0; printf("line %s\n", *input); while(input[i] != "\0"){ printf("character %c\n", *input[i]); if(*input[i] >= 'a' && *input[i] <= 'z'){ *input[i] = tolower(*input[i]); } i++; } } int main(int argc, char *argv[]) { file *file = fopen(argv[1], "r"); char linebyline [50], *linestr = linebyline; char test; glutinit(&argc, argv); while(!feof(file) && file != null){ fgets(linestr , 100, file); makelower(&linestr); printf("%s",linestr); //directfile(); } fclose(file); glutmainloop(); }

i see more problems now, extend comments answer:

you allocate array of 50 characters, tell fgets 100 characters, might fatal fgets overwrite memory not in string.

when passing c string function, don't have pass address of pointer string (&linestr), actual pointer or array okay. means can alter makelower function void makelower(char *input) or void makelower(char input[]). right argument makelower declared array or char pointers, not pointer array of char.

in new makelower proposed above, can access single characters either array (input[i]) or pointer plus offset (*(input + i). said in comment, lastly version compiler create if utilize first. first more readable suggest that.

also in makelower create comparing "\0", string , not character. right actually: should utilize input[i] != '\0'.

and how implement it:

void makelower(char *input) { while (*input != '\0') /* "while (*input)" work */ { *input = tolower(*input); input++; } }

a few explanations function:

all char arrays can converted char pointer, not other way around. passing char pointer mutual way pass string actually, can see standard functions accepts strings (like strlen or strcpy.) the look *input dereferences (i.e. takes value of pointer points to) string. same *(input + 0) , value of first character in string. while first character in string not '\0' (which technically normal zero) loop. get first character of string , pass tolower function. work no matter character is, tolower turn upper case characters lower case, other characters returned were. the result of tolower copied on first character. works because right hand side of assignment must executed before assignment, there not error or problem. last increment pointer one. create input point next character in string. works because input local variable, operations on pointer not impact in calling function.

this function can called this:

char input[100]; fgets(input, sizeof(input), stdin); printf("before: \"%s\"\n", input); makelower(input); printf("after : \"%s\"\n", input);

c visual-studio-2010 file-io

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -