Monday, May 24, 2010

I need quick help with turbo c++ programing, please?

I need to make a program that when I insert a word like 'hello'


it will capitalize the odd number letter, and minimize the even numbered letters. Meaning, when you insert 'hello', it will come out as 'HeLlO'





I have this code until now





#include %26lt;string.h%26gt;


#include %26lt;stdio.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;conio.h%26gt;





int main(void)


{


int length, i;


char *string;


clrscr();


printf("gimme string\n");


gets(string);


length = strlen(string);


for (i=0; i%26lt;length; i++)


{


if(strlen(string)%2==1)


{


string[i] = toupper(string[i]);


}


else


{


string[i] =tolower(string[i]);


}


}





printf("%s\n",string);





getch();


return 0;


}

I need quick help with turbo c++ programing, please?
First of all, you declare string but never set aside memory for it. You either have to do a char string[80] or, if you MUST do a char *string;, add the lines:





string=(char *)malloc(80*sizeof(char));


Which gives me the warning:


convert.c:9: warning: incompatible implicit declaration of built-in function ‘malloc’


and


free(string);





Second, the line:





if(strlen(string)%2==1)





means that if the length of the string is odd ALL the letters will be capitolized. If it is even, ALL the letters will be small. It should be:





if (i%2==1)





Then it should work. Oh, whatever your teachers tell you, conio.h is evil. Try system("pause");


It's in stdlib.h .


No comments:

Post a Comment