Reputation: 135
Does anyone would help me with this code ? The function needs to do like in topic is written. Is it ok ? What is more I need is to count the number of made changes. How to implement this one ?
int change(char *path){
FILE *f = fopen(path, "r");
if(f==NULL){
printf("Error...");
return -1;
}
int text, length;
fscanf(f, "%s", &text);
length = strlen(text);
for(i = 0; i < length; ++i){
if(islower(text[i]))
{
text[i] = toupper(text[i]);
}
if(isupper(text[i]))
{
text[i] = toslower(text[i]);
}
fprintf(f,"%s",text);
fclose(f);
Upvotes: 2
Views: 3632
Reputation: 490497
To count the number of changes, simply create a variable (int count = 0
) and increment it with every change (count++
).
int change(char *path){
FILE *f = fopen(path, "r");
if(f==NULL){
printf("Error...");
return -1;
}
int text, length;
int count = 0;
fscanf(f, "%s", &text);
length = strlen(text);
for(i = 0; i < length; ++i){
if(islower(text[i]))
{
text[i] = toupper(text[i]);
count++;
}
if(isupper(text[i]))
{
text[i] = tolower(text[i]);
count++;
}
}
fprintf(f,"%s",text);
fclose(f);
}
Upvotes: 1
Reputation: 5773
Right now your code will first try to change the text from lower case to upper case, and then if this succeeds change it back to lower case. I don't think that's what you want since you now have two cases, either it changes from lower to upper and back to lower or it doesn't change at all.
To track the changes we add a variabled "changes" which we initialize to zero.
Instead if you want to change the character to upper case if it's lower case and to lower case if it's upper case rewrite it like this:
if(islower(text[i])) {
text[i] = toupper(text[i]);
changes++;
} else if(isupper(text[i])) {
text[i] = tolower(text[i]);
changes++;
}
There's also a spelling mistake, toslower(text[i]) but I assume you meant tolower(text[i])
Upvotes: 1