O
O
OccamaRazor2016-09-09 14:29:59
Programming
OccamaRazor, 2016-09-09 14:29:59

How to find out in C whether the entered 1) string 2) character is in upper case or lower case?

I can’t figure out how to make a number out of a letter, that is, so that the character matches the character in the ASCII table; and then, how to use an array to make data input, that is, not char but a full-fledged string, so that a string is entered immediately and not a character

char name;
scanf("%s",name);
for (i=1;i<=strlen(name);i++){
if (name >= 97 && name <= 122)
    	{ 
           printf("lower");
           name -=32;
           printf("now it is upper");
           }
if (name >= 65 && name <=90)
         	{ 
           printf("upper");
            name +=32;
             printf("now it is lower");
           }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lofem, 2016-09-09
@Lofem

Just declare the variable as char name[100];
After that, refer not to name, but to name[i]
Well, to get the numeric value of the symbol, it is enough to write int (c) or in your case int (name[i])

M
Mercury13, 2016-09-09
@Mercury13

> how to use an array to make data input, that is, not char but a full-fledged string, so that a string is entered immediately and not a character

char s[100];
scanf("%99s", s);

Well and at the same time as to process this line.
len = strlen(s);
for (i = 0; i < len; ++i) {
  name = s[i];
  // обработай как-нибудь наш name…
}

Further I will kick your code.
> scanf("%s",name);
Nasty feature of C as an educational language. To do simple things, you need to know complex concepts. All scanf options matching substitutions - %d, %s, etc. - pointers. And the type of this pointer corresponds to the type in the format string.
Because of this, your code is explicit AV (Access Violation); to enter one character, you need scanf("%c", &name);. To enter a bunch of characters - I wrote above.
Why doesn't s need an address sign? But because an array is initially a pointer.
The second and third nasty chips are the fundamental insecurity of many functions and the difficulty in organizing a simple console dialogue. Let's say C out of the box does not allow you to expand the line as you type, and it is important to limit the length of the input, otherwise you enter 100 characters - it will be AV at best. And if we enter more than 99 characters, we count 99 and stop, and further scanfs will start reading the “tail”.
> for (i=1;i<=strlen(name);i++) {
Explicit O(n²) CPU time from scratch. strlen looks for the terminating '\0', i.e. its complexity is O(n). It is necessary to store the length in a separate variable.
The second way is also possible - through pointers.
char* p;
for (p = s; *p != '\0' ++p) {

This is more efficient, but also more advanced.
> if (name >= 97 && name <= 122)
Why not write like this if (name >= 'a' && name <= 'z'):? The constants 'a' and 'z' are the same int in C, char in C++ (probably for compatibility with function overloading).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question