Nikhil Dholeshwar
Nikhil Dholeshwar

Reputation: 1

To print characters, number and special characters one by one in c++

I want to print characters, numbers, and special characters one by one below each other, but having an error in taking string.

#include<iostream.h>
#include<string.h>

main()
{
   string s = "124#$&afhj";
   int i;
   for(i=0;i<s.length();i++)
   {
      if((s[i]>=65&&91>=s[i])||(s[i]>=95&&123>=s[i]))
      {
         cout<<s[i]<<endl;
      }
      else
      {
        if(s[i]>=0&&s[i]<=9)
        {
            cout<<s[i]<<endl;
        }
        else
        {
            cout<<s[i];
        }
      }
   }
}

Upvotes: 0

Views: 2306

Answers (2)

Christian Hackl
Christian Hackl

Reputation: 27538

  • You are using a non-standard header <iostream.h>. It must be <iostream>. You aren't using an ancient compiler from the 1990s, are you? There are excellent free and modern C++ compilers available for Windows and Linux. Download one and use it.
  • <string.h> exists but is not the correct header to get std::string; it instead contains C-style char* functions and is not what you want here.
  • main must return int, even though a return 0 statement is not necessary.
  • You cannot use string, cout or endl without the std:: prefix. So make it std::string, std::cout and std::endl. Do not use using namespace std.
  • Do not use std::endl but '\n'.
  • C++ does not guarantee ASCII, and it does not even guarantee that letters are consecutive (it does guarantee that for digits, see below). While it's unlikely for you to encounter a system which is not ASCII-compatible, you should still get into the habit of writing safe, portable code. Which means that you should not use magic numbers like 65 but use 'A' instead. And of course, your >= comparisons are not guaranteed to work. Getting this right is surprisingly difficult in C++. You can use std::isalpha but must pay attention not to invoke undefined behaviour. See also a more detailed treatise on this subject.
  • 91 is ASCII for ']'. You certainly meant 90 for 'Z'. 95 and 123 are likewise off. Have a look at http://www.asciitable.com/. But, see above, do not use the magic numbers and do not use >= comparisons.
  • Your code could use more spaces to make it more readable.
  • Why declare i outside of the loop? It needlessly increases its scope. And it has the wrong type; it's a signed int when it should be an unsigned one, or simply std::string::size_type. However, you should consider using a range-based for loop to iterate through the string anyway.
  • Testing a character for 0 and 9 does not make sense. You want to test for '0' and '9'. Note how in this case, C++ does guarantee consecutive values for the characters even if ASCII is not guaranteed. You can therefore use range comparisons safely.

Here we go:

#include <iostream>
#include <string>
#include <cctype>

int main()
{
   std::string s = "124#$&afhj";
   for (auto&& character : s)
   {
      if (std::isalpha(static_cast<unsigned char>(character)))
      {
         std::cout << character << '\n';
      }
      else
      {
        if (character >= '0' && character <= '9')
        {
            std::cout << character << '\n';
        }
        else
        {
            std::cout << character;
        }
      }
   }
}

This will work. I don't think it's really useful, because what it does is:

  • Print digits and letters with a linebreak.
  • Print everything else without a linebreak.

But after all, that's what the original program's intention was.

Upvotes: 2

Lukasz Re
Lukasz Re

Reputation: 79

In your code is many mistakes. In my opinion your code should look like:

#include<iostream>
#include<string>
using namespace std;

int main() {
    string s = "124#$&afhj";
    unsigned int i;
    for (i = 0; i < s.length(); i++) {
        if ((s[i] >= 65 && 91 >= s[i]) || (s[i] >= 95 && 123 >= s[i])) {
            cout << s[i] << endl;
        } else {
            if (s[i] >= 0 && s[i] <= 9) {
                cout << s[i] << endl;
            } else {
                cout << s[i];
            }
        }
    }
}

Upvotes: -1

Related Questions