-
total beginner!!!
Hi there,
could someone help me please, Ive just stared with c and I want the function to accept upto 50 characters but finish when i hit return and then to change the case of them.
It continues to accept characters after i have hit return up until the array of 50 is finished.
My next problem is that it seems to loop outside the loop and repeat the whole process again from the initial printf statement.
any suggestions would be great.
MAX_LINE_LENGTH = 50
Code:
int inverseCharacters()
{
char ch[MAX_LINE_LENGTH];
int i= 0;
printf("%s", "\nEnter a line of text combining upper and lower case: \n");
//scanf("%s", &ch);
while (i <= MAX_LINE_LENGTH)
{
ch[i]= getchar();
if(islower(ch[i]))
{
ch[i] = toupper(ch[i]);
putchar(ch[i]);
}
else
{
ch[i] = tolower(ch[i]);
putchar(ch[i]);
}
i++;
}
return 0;
}
-
Read this to learn about <return> and getchar().
If you are seeing the initial printf() more than once, then inverseCharacters() is being called more than once.
gg
-
Hmmm, few comments.
>>//scanf("%s", &ch);
I think you meant without the & operator.
Also, for getting string from a stream, use fgets().
-
>while (i <= MAX_LINE_LENGTH)
Here you are getting MAX_LINE_LENGTH+1 characters instead of MAX_LINE_LENGTH, because you are starting from i = 0.
-
You don't really need all of that coding to convert a string to either all upper or all lower case, and reading it in as opposed to what I did would not be significantly different.
Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
int i;
char ch[] = "aBcDeFgHiJk";
printf("%s\n", ch);
for(i=0; (ch[i] = toupper(ch[i])) != '\0'; i++)
putchar(ch[i]);
putchar('\n');
for(i=0; (ch[i] = tolower(ch[i])) != '\0'; i++)
putchar(ch[i]);
return 0;
}
-
help!!!
I've sorted out where it was calling the function the second time, thanx.
I still cant seem to work out how to get my code to accept less than the maximum amount of characters [50] one line. eg. i input 10 characters then hit <return>, it inverses those, but will then continue to receive input for the remaining [50] spaces which i DON'T want. I only want the 1 line of text to be accepted and inversed.
Code:
int inverseCharacters()
{
char ch[MAX_LINE_LENGTH];
int i= 0;
printf("%s", "\nEnter a line of text combining upper and lower case: \n");
while (i < MAX_LINE_LENGTH)//while_2
{
ch[i]= getchar();
while (ch != '\n')//while_1
{
if(islower(ch[i]))
{
ch[i] = toupper(ch[i]);
putchar(ch[i]);
}//endif
else
{
ch[i] = tolower(ch[i]);
putchar(ch[i]);
}//endifelse
i++;
}//endwhile_1
}//endwhile_2
printf("%s", "\nPress enter to return to the menu: \n");
//menu();
return 0;
}
I have tried "while (ch != '\n')" but that just gave me an error 'operands of != have illegal types 'pointer to char' and 'int'
Any help greatly appreciated!!
-
Just a little info:
char c[50];
that only holds a 49 character string. Don't forget the null byte at the end of an array! ('\0') If you want 50, you need an array of 51. (correct me if I'm wrong here.)
-
I finally got it!!
Thanks for your replys, appreciate it. I finally ended up using
[code]
fgets(ch, MAX_LINE, stdin);
for(i = 0; i < MAX_LINE, ch[i] !='\0'; i++)
{
if(islower(ch[i]))
{
...
}
else
{
...
}//end_if_else
putchar(ch[i]);
}
[\code]