Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Description

Enter N strings in a row (-1 ends to exclude -1), and the lowercase English letters in the string are converted to uppercase and printed out in the order of input

Input format

String 1 (can contain blanks) String... (can contain blanks) String N (can contain blanks)
Output format

Capitalized string 1 (line feed) The string after capitalization... (line feed) Capitalized string N (line feed)

What I have tried:

#include <stdio.h>
 #include<string.h>
 #include<ctype.h>
 int main()
 {
     char str1[100];
     
     gets(str1);
     
     int i;
     for(i=0;i,100;i++){
         if(str1[i]>='a'&str1[i]<='z')
             str1[i]-=32;
         if(str1[i]=='\0')
             break;
     }
     puts(str1);
 return 0;
 }
Posted
Updated 23-Apr-21 19:53pm

& is a binary AND, not a logical AND - to combine two conditions like this this:
(a == b) AND (c == d)
You need the logical AND operator which is two ampersands: &&
C
(a == b) && (c == d)


Please do yourself a favour and be consistent in your bracket usage and indentation: pick a single style and use it throughout.
When you get code like this:
C++
int main()
{
    ...
    for(...){
        if(...)
            ...
        if(...)
            ...
    }
    ...
...
}
It shows you don't care about what you are doing, and makes code very hard to read. Pick a style, be consistent:
C++
int main(){
    ...
    for(...){
        if(...)
            ...
        if(...)
            ...
    }
    ...
    ...
}
Ugly, horrible and vile though it is. Get the indentation wrong, and it's a PITA to work out where the missing bracket is or should be ...
Or
C++
int main()
{
    ...
    for(...)
    {
        if(...)
            ...
        if(...)
            ...
    }
    ...
    ...
}
Commonly used. Much better.
Or
C++
int main()
    {
    ...
    for(...)
        {
        if(...)
            ...
        if(...)
            ...
        }
    ...
    ...
    }
My preference - Whitesmiths.

In addition, I'd strongly recommend that as a beginner you always use curly brackets, even when they aren't strictly necessary - if you don't it can make for strange bugs later when you edit it!
C++
int main()
    {
    ...
    for(...)
        {
        if(...)
            {
            ...
            }
        if(...)
            {
            ...
            }
        }
    ...
    ...
    }
It may seem irrelevant, but after you've spent a couple of hours trying to work out why this doesn't work
C++
if (a == b)
   c = c + 1;
   d = d + 10;
And realize it should be
C++
if (a == b)
   {
   c = c + 1;
   d = d + 10;
   }
you will see why it's a good idea!
 
Share this answer
 
The gets() function was removed from the language a decade ago (C11) and has been deprecated for twice that long (since C99). It's still supported by compilers, but shouldn't be taught--and has *never* been a good choice for use in production code.

Your for statement is misleading. The i,100 is syntactically valid, but doesn't do anything other than evaluate as "true" no matter what i happens to be. If the idea was to stop after 100 characters--even if no terminating zero byte was seen--then here's an idea that will do that, and save you from testing for the zero byte in the loop body:
int i;
char c;
fgets(str1, sizeof str1, stdin);
for (i=0; i<100 && 0!=str1[i]; ++i)
{
    if (str1[i] == '\n'):
    {
          str1[i] = 0;
          break;
    }
    if (islower(str1[i]))
        str1[i] = toupper(str1[i]);
}

I've used the islower and toupper() functions, since you've already included <ctype.h>. Those shorten the test for lower case letters on input and shifting them to upper case.

That also shows use of fgets() as the long-preferred alternative to the old gets() function. Most compilers should have implemented gets_s() by now (added in C11) and if your compiler has it, then use:
gets_s(str1, sizeof str1);

...and then delete the if statement testing for a \n newline character.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900