Topic: "Learning C++" (page 1 of 1)

1
Author Post
moose
groupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Hi,

I thought it's time to me to learn C++. To train, I've took a look at TopCoder and just found this exercise ( SRM 100 - Motorola Single Round Match 100 Round 1 - Division II, Level One, linkLink):
Problem Statement
    	A letter-string is composed of letters ('A'-'Z','a'-'z') and dashes ('-'). The length of a letter-string is the number of characters in it not including dashes (in other words, the number of letters in the string). Given a list of letter-strings you will return the sum of their lengths. 



Create a class LetterStrings that contains the method sum, which takes a String[], s, and returns an int representing the sum of the lengths of the given letter-strings.
 
Definition
    	
Class:	LetterStrings
Method:	sum
Parameters:	String[]
Returns:	int
Method signature:	int sum(String[] s)
(be sure your method is public)
    
 
Constraints
-	s will contain between 1 and 50 elements, inclusive.
-	Each element of s will have length between 1 and 50, inclusive.
-	Each element of s will contain only letters ('A'-'Z','a'-'z') and dashes ('-').
 
Examples
0)	
    	
{"-"}
Returns: 0
1)	
    	
{"A"}
Returns: 1
2)	
    	
{"-----Abc"}
Returns: 3
3)	
    	
{"-A-B-C-D", "--------EFGHI", "JKLMNOPQR", "---STU-VW-XYZ"}
Returns: 26


Now I've created this piece of code:
#include <iostream>
#include <string>

using namespace std;

class LetterStrings {
    public:
        int sum(string s[]);
};

int LetterStrings::sum(string s[])
{
    int summe = 0;
    for(int el=0; s[el] != "\0"; el++){
        for(int i=0; i < s[el].length(); i++){
            if( isalpha(s[el][i]) ) {
                summe += 1;
            }
        }
    }
  return summe;
}

int main()
{
    LetterStrings a, b;
    string str[5] = {"-A-B-C-D", "--------EFGHI", "JKLMNOPQR", "---STU-VW-XYZ"};
    cout << a.sum(str) << endl;
}


It seems to work, but I'm not quite sure if it fits the requirements (Method signature: int sum(String[] s)?)

What could be done better in this code?

edit: Just found the linkstatistics with the top-solution:
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <utility>
using namespace std;
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define VI vector<int>
#define VVI vector<vector<int> >
#define VS vector<string>
#define si size()
#define len length()
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
class LetterStrings
{
  public:
  int sum(vector <string> s)
  {
    int ret=0;
    for(int c=0;c<s.si;c++)
      for(int d=0;d<s[c].len;d++)
        if(isalpha(s[c][d]))
          ret++;
    return ret;
  }
};
 
// Powered by PopsEdit

isalpha is obviously better then my check via ASCII-Table codes (I've edited my solution), but the top-solutions seems to include quite a lot which isn't needed.
Edited by moose on 08.09.2011 11:01:45
private message EMail Website
dloser
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Hi moose,

QuoteQuote from moose:
I thought it's time to me to learn C++.

Oh boy... ;)

QuoteQuote:

It seems to work, but I'm not quite sure if it fits the requirements (Method signature: int sum(String[] s)?)

I suspect the exercise was made with Java in mind. For C++ using vector<string> is probably closest to String[], both being classes with similar methods.

QuoteQuote:

What could be done better in this code?

QuoteQuote:

class LetterStrings {
    public:
        int sum(string s[]);
};


The whole class thing is probably just because of Java, but in any case making the method sum static would be appropriate here, I think.

QuoteQuote:

    int summe = 0;


Not sticking with English... :P (Sorry.)

QuoteQuote:

    for(int el=0; s[el] != "\0"; el++){


Just using "" here would work as well. Nevertheless it shows that the whole string[] business makes things ugly. For example, an empty string in the input cuts the program short. Using just char*[] (or even string*[], ugh) at least allows you to end the array with NULL (which is distinct from "").

QuoteQuote:

    LetterStrings a, b;


Nitpicking: unused b.

QuoteQuote:

isalpha is obviously better then my check via ASCII-Table codes (I've edited my solution), but the top-solutions seems to include quite a lot which isn't needed.

Yeah, I think that's just a bunch of standard stuff that author includes in every file.
private message
moose
groupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Thanks.

Now my next questions:
As a python programmer I'm used to have integers which are always as big as I need them. In C++ the biggist "standard int" type seems to be "unsigned long long" which is in [0;9223372036854775807] which is [0;2^63-1].
What do I do if I need longer integer types?

How do I convert an unsigned long long to a string?
Edited by moose on 08.09.2011 11:02:13
private message EMail Website
dloser
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
You'll probably want to use one of the available libraries for that (e.g. linkGMP).
private message

Topic: "Learning C++" (page 1 of 1)

1