+ 2
How to input 10 names from user and print the names in capital letter ?
Any Hint or a whole solution is appreciated:-)
4 Antworten
+ 3
I am sorry, last thing I wanted to do is to confuse you. Let's see, you are not allowed to use anything besides stdio.h, right? Okay, I think we can manage....
Now, the string.h header provides nice tools to work with strings (and arrays) and ctype.h is recommended when dealing with character types because it abstracts from the underlying implementation and locale (i.e. which characters are considered "lower case" ? ). But be that as it may....
We'll just stick with 7bit ASCII and the English alphabet. If you look at the table (for example: http://www.neurophys.wisc.edu/comp/docs/ascii/ ) then you'll notice that the lower case letters have a value 32 greater than their respective capital letters. We can use that fact by switching that bit off if we have a lower case letter. Switching a bit off is done using XOR (the caret symbol: ^ ). (If that is too basic you might as well just subtract 32 :) )
Now, prepare a buffer to store user input. say, "char buf[20];" should probably do it. Then you need a loop to run 10 times. Inside that loop, use scanf in the stdio.h header to read the input. Why "scanf" and not "fgets" ? Because "scanf" removes the newline character, which "fgets" doesn't do. Using fgets we would need a function from the string header to remove the newline character.
Alright, after reading, check the first character and check if it lies between 'a' and 'z' (the lower case characters). If so, flip the switch (or subtract 32). All this could look as follows:
int i;
for(i=0; i < 10; ++i)
{
scanf("%20s", buf);
if(buf[0] >= 'a' && buf[0] <= 'z')
{
buf[0] = buf[0] ^ 32;
}
printf("%s\n", buf);
}
+ 2
If you don't have strupr() available (it is non-standard), here is a simple implementation which upcases a string in place(!) [it means you cannot run it on constant strings]. It's not that bad since you have to read the names into some sort of character buffer anyway ;)
#include <ctype.h>
char* strupr(char* s)
{
char *p;
for(p = s; *p; p++)
*p = toupper(*p);
return s;
}
+ 2
Leif sorry but kinda confused me whatever you wrote ,I am still learning basics
+ 2
I just want to input different names in one string and if those names are in small letters print them in capital letters in C using stdio as header file and without any string function!,