3 Replies - 718 Views - Last Post: 11 May 2014 - 09:08 AM Rate Topic: -----

#1 stdONuos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-May 14

how i can cout the string characters themselves in c++ !

Posted 11 May 2014 - 08:46 AM

hey!

am working with arrays and i cant get to the write solution to cout the characters.

the Question is : Write a program that inputs a character string from the user as an input (using cin.get())and store it in myStr[ ] array.Assume that the length of the input string is less than 100 characters and the string may contain lower case, upper case, and/or symbols.The program should then print the number of lower case, upper case, digits, and symbols along with the characters themselves.

my code :
 
#include <iostream>
#include <string>
#include<iomanip>

using namespace std;

int main()
{
    int ctr1=0, ctr2=0, i=0, ctr3=0;
    char myStr[100];

    cout<<"Enter a string consisting of lower case and upper case characters:"<<endl;
    cin.get(myStr,100);

    while(myStr[i]!='\0')
    {
        if(myStr[i]<='z' && myStr[i]>='a')
            ctr1++;
        if(myStr[i]<='Z' && myStr[i]>='A')
            ctr2++;
     if(ispunct(myStr[i]) || isspace(myStr[i]))
            ctr3++;

        i++;

        cin.get(myStr,100);
    }


      cout<<setw(2)<<"Number of lower case characters = "<<ctr1<<setw(5)<<"    which are :"<<endl;
      cout<<setw(2)<<"Number of upper case characters = "<<ctr2<<setw(5)<<"    which are :"<<endl;
      cout<<setw(2)<<"Number of symbol characters = "<<ctr3<<setw(5)<<"    which are :"<<endl;


 return 0;
}


what i have now, how can i cout the uppercase characters and the lower case characters and the symbol characters !

This post has been edited by snoopy11: 11 May 2014 - 08:53 AM
Reason for edit:: added code tags please learn how to use them


Is This A Good Question/Topic? 0
  • +

Replies To: how i can cout the string characters themselves in c++ !

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: how i can cout the string characters themselves in c++ !

Posted 11 May 2014 - 09:04 AM

You forgot to implement numbers,

but you need 4 arrays of type char would do but since this is C++ you would be better using std::string

so

std::string lowercase[100];
std::string uppercase[100];
std::string digits[100];
std::string symbols[100];


then in your loop of myStr

when you encounter a type say for argument sake lowercase

you would do something like

lowercase[ctr1]= myStr[i];
ctr1++;


Then when finished counting lower, upper, digits and symbols output each type at the end separated by comma or space.

Regards

Snoopy.

This post has been edited by snoopy11: 11 May 2014 - 09:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 TgaJava  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 28-April 14

Re: how i can cout the string characters themselves in c++ !

Posted 11 May 2014 - 09:06 AM

I don't think the code you posted is yours; because if it was yours then you wouldn't ask such a question.

Anyway to do your question you can use the another while loop similar to the one you posted, and change rearrange your if-conditions.
for example
//pseudo code
lowercasecounter
uppercasecounter
if(str[i] is a upto z) increment lowercasecounter
if(str[i] is A upto Z) increment uppercasecounter 
//do the same for punctuations
finally display those variables



I don't think the code you posted is yours; because if it was yours then you wouldn't ask such a question.

Anyway to do your question you can use another while loop similar to the one you posted, and change rearrange your if-conditions.
for example
//pseudo code
lowercasecounter
uppercasecounter
if(str[i] is a upto z) increment lowercasecounter
if(str[i] is A upto Z) increment uppercasecounter 
//do the same for punctuations
finally display those variables



I don't think the code you posted is yours; because if it was yours then you wouldn't ask such a question.

Anyway to do your question you can use another while loop similar to the one you posted, and change rearrange your if-conditions.
for example
//pseudo code
lowercasecounter
uppercasecounter
if(str[i] is a upto z) increment lowercasecounter
if(str[i] is A upto Z) increment uppercasecounter 
//do the same for punctuations
finally display those variables


Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: how i can cout the string characters themselves in c++ !

Posted 11 May 2014 - 09:08 AM

Someone else correct me if I'm wrong, but I'm pretty sure the OP is missing an include file, specifically, <cctype>, which includes the is*() family of functions for determining the type of a character.

I have to agree with the above poster that if the OP wrote this code, then they probably wouldn't be asking these questions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1