10 is the ascii value for a newline '\n'
When you hit enter a newline is appended to the buffer.
fgets then appends a null to terminate the string.
You are processing the newline as part of the buffer, which it is.
10 is the ascii value for a newline '\n'
When you hit enter a newline is appended to the buffer.
fgets then appends a null to terminate the string.
You are processing the newline as part of the buffer, which it is.
Last edited by rmetcalf; 09-16-2008 at 12:17 PM.
Try this:
My output was:Code:char input[51]; int i; printf("Enter something...\n"); scanf("%[^\n]", input); printf("\n"); // newline for(i = 0; i < strlen(input); i++) printf(" %c = %d\n", input[i], input[i]); printf("\n\nPress any key to continue..."); getch();
Also, your other piece of code needs to be modified.Code:Enter something... sentence s = 115 e = 101 n = 110 t = 116 e = 101 n = 110 c = 99 e = 101 Press any key to continue...
Code:for(x = 0; x < strlen(word); x++) { word[x] = toupper(word[x]); printf("%c", word[x]); }
Last edited by george_1988; 09-16-2008 at 06:22 PM.
Thank you !
Actually on that second part , I am trying to have the string alternate case , so ..
user inputs : dog
the out put would be : DoG
I cant seem to get it to work in a loop though ? I have tried something to the effect
Any suggestions ? As you can see , my first attempt was gut to read and apply the tolower or toupper to the array index but if I dont fill the array I get a bunch of output that is junk.Code:char c = 'A'; char lower = tolower(c); char upper = toupper(lower);
ok, thank you for any suggestions !
ggraz,
To achieve that you will need to use two loops. Look at the following code:
My output:Code:#include <stdio.h> #include <ctype.h> /* for toupper() & tolower() */ #include <string.h> /* for strlen() */ int main(void) { char input[51]; int i; printf("Enter something...\n"); scanf("%[^\n]", input); /* convert every character to uppercase first */ for(i = 1; i < strlen(input); i++) input[i] = toupper(input[i]); /* convert every other character to lowercase */ for(i = 1; i < strlen(input); i++) input[i*2] = tolower(input[i*2]); printf("%s", input); getch(); /* to view output */ }
It is also worth noting if you add spaces in your string, your upper, lower, upper, lower, will be redundant as the space will be read as a character and will get converted to upper/lower.Code:Enter something... sentence sEnTeNcE
For example, see the following output:
Note the two capital letters in a row before and after the space. See if you can correct this issue. You might want to use the isspace() function.Code:Enter something... sentence sEnTeNcE TeSt
Last edited by george_1988; 09-17-2008 at 06:06 PM.
ok, I see my error ! Thank you so much ! I was trying to do it with only one loop ! thus the program was giving me fits ! Always learning ! Thank you !
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.