for the love of....

drspangle

New member
Right, dead easy: a basic string to lowercase converter, I`ve used it a thousand times, it`s practically identical to ones I`ve found on the net, same as lecture notes etc etc etc. I will point out again, I have been using this function for ages, with never a ptoblem. THEN WHY IN THE HOLY HELL IS IT SEGFAULTING NOW?:mad:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void lowercase(char *string);

int main(void){

	lowercase("FRED");
	return(0);

}

void lowercase(char *stringIn)
{
	int counter = strlen(stringIn);
	printf("You inputted: %s\n",stringIn);
	while (*stringIn != '\0'){
		*stringIn = tolower(*stringIn); [COLOR="Red"]<======== here![/COLOR]
		*stringIn++ ;
	}	
	stringIn-=counter;

	printf("New string is: %s\n",stringIn);

}

I`ve wasted 2 hours with this, and it`s retarded because It`s copied and pasted code that I`ve used a million times.

Any ideas? I will reboot in the vain hope that something has gone retardedly wrong somewhere :confused:
 
No luck with that, works if I deal it as a vector though, needs some jiggery to take a string pointer in though...

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void toLowerCase(char *string);

int main(){

	toLowerCase("DAAVID");
	return(0);

}

void toLowerCase(char *in)
{
	char david[8];
	int i,n=strlen(in);

	for(i=0;i<n;i++){
		david[i] = *in;
		in++;
	}
	david[n] = '\0';

	char *tmp = david;

	printf("Received: %s\n",tmp);
	int counter=strlen(tmp);
	while(*tmp != '\0'){
		*tmp = tolower(*tmp);
		*tmp++;
	}
	tmp-=counter;
	printf("Produced: %s\n",tmp);
}
 
Last edited:
try changing
Code:
int main(void){

    lowercase("FRED");
    return(0);

}

to

Code:
int main(void){
    char test[] = "FRED";
    lowercase(test);
    return(0);

}


and that *stringIn++ in your while loop should probably be just stringIn++
 
Actually, I tried compiling your version under Borland C++ Builder and after adding a
#include <ctype.h>
it compiled fine and ran as intended.

Change that *stringIn++; though. It works, because the ++ has a higher priority than the *.
 
heh, gotta love pointers eh? I guess Sun was on to something when getting rid of that in Java (though yes objects still technically use them blah hehe).

-Brian
 
absolutefunk said:
heh, gotta love pointers eh? I guess Sun was on to something when getting rid of that in Java (though yes objects still technically use them blah hehe).

-Brian

I don`t mind pointers all that much, but C would be my favourite language if it included some more decent string tools.
 
drspangle said:
I don`t mind pointers all that much, but C would be my favourite language if it included some more decent string tools.
that's what prompted me to ditch C and use C++ for PC related stuff. C is still important on lesser hardware, like a ti-89 :bleh:
 
seeker010 said:
that's what prompted me to ditch C and use C++ for PC related stuff. C is still important on lesser hardware, like a ti-89 :bleh:

I most likely won`t have a choice, as I`ll be dealing with chips that have built in C interpreters (as opposed to assembly of some sort). In those instances strings arn`t all that useful :D

My next project starting this week is a p2p instant messenger (so no central server, hopping between nodes etc - looks tricky) is going to be in python, so that should be a welcome change :)
 
Back
Top