d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Seeking Some Critique And Answers To String Stuff
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 9 2012 03:01am
So today I made this just to mess around and see if it worked the way I wanted it to (it did)

Code
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;

bool numchecker(string x, int len);

int main()
{

   string x;
   int len;

   cout << "Enter string plz." << endl;

   cin >> x;

   len = x.length();

   if (numchecker(x, len))
   {
       cout << "Your string contains no integers!" << endl;
   }

   return 0;
}

bool numchecker(string x, int len)
{
   int ret = 0;
   for (int i = 0;i < len; i++)
   {
       if (isdigit(x[i]))
       {
           ret++;
       }
   }
   if (ret > 0)
   {
       cout << "Error: there were " << ret << " integers located in the string!" << endl;
       return false;
   }
   return true;
}


It's been awhile since I made anything but wondering if anyone could critique me on this. (as far as like, type of loop I used and efficiency etc)

Also, I had a question regarding C-strings. Namely, using the strlen function located in the string.h lib.

Basically, it returns a size_t data type. Do I have to like, declare a variable as size_t to utilize that number?

In the future in my class I know we will be using c-strings a lot, so I am curious as to what exactly this size_t deal is. Could I create an integer var and then have the value returned by strlen put into it?

ex:

char x[100];
int len;

len = strlen(x);

This post was edited by Eep on Sep 9 2012 03:03am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 9 2012 10:03am
it looks okay, but if it were me id skip using the isdigit function and just use
Code
if((int)x[i] >= 48 || (int)x[i] <= 58)
{
  ret++;
}


compares the current characters integer value to its ascii decimal integer value.

either way works tbh.

i think you can use any integer datatype for strlen. the only reason to use something other than integer would be if your string contains a higher number than what a integer value can hold. in which case you would have to use a different data type for holding numbers.

in the cplusplus function information section they just output the output of strlen to printf using %u and casting the function output as a unsigned integer

just for shits and giggles if you wanted you could take the integer output from strlen and cast it as a character and use that rofl. could also use a simple algorithm so create a string of chars that fall between a-z to simulate 1-25. ex:
"c" would be 3, and "zb" would be 27.

just saying that you dont necessarily need to follow the correct data types. it just makes life easier if you do.


edit:: also give your variables realistic names. if you work on an actual project and name 10 strings x, asd,afgjasghdjkansdghasd youll be hated lol (you prolly already do but its a good habit to do 24/7)

This post was edited by AbDuCt on Sep 9 2012 10:10am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 9 2012 01:02pm
Yeah my variable naming sucks still. Could I, for example, put the out put of strlen into a char then use itoa on it?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 9 2012 02:54pm
Quote (Eep @ Sep 9 2012 03:02pm)
Yeah my variable naming sucks still. Could I, for example, put the out put of strlen into a char then use itoa on it?

yes, the whole character string thing was just a joke (as in encrypting the output as alphabet characters), but if you needed you could do that since itoa turns a integer into a string.

the call would be something like

Code
itoa(strlen(x), buffer, 10);


and if strlen returned 97 buffer would hold "97" where buffer[0] would equal "9" and buffer[1] would equal "7"

Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 9 2012 11:25pm
yeah I need t learn more about the 'to' functions.
Member
Posts: 17,028
Joined: Jan 18 2005
Gold: 15,778.00
Sep 24 2012 09:41am
Quote (AbDuCt @ Sep 9 2012 10:03am)
it looks okay, but if it were me id skip using the isdigit function and just use
Code
if((int)x[i] >= 48 || (int)x[i] <= 58)
{
  ret++;
}


compares the current characters integer value to its ascii decimal integer value.

either way works tbh.

i think you can use any integer datatype for strlen. the only reason to use something other than integer would be if your string contains a higher number than what a integer value can hold. in which case you would have to use a different data type for holding numbers.

in the cplusplus function information section they just output the output of strlen to printf using %u and casting the function output as a unsigned integer

just for shits and giggles if you wanted you could take the integer output from strlen and cast it as a character and use that rofl. could also use a simple algorithm so create a string of chars that fall between a-z to simulate 1-25. ex:
"c" would be 3, and "zb" would be 27.

just saying that you dont necessarily need to follow the correct data types. it just makes life easier if you do.


edit:: also give your variables realistic names. if you work on an actual project and name 10 strings x, asd,afgjasghdjkansdghasd youll be hated lol (you prolly already do but its a good habit to do 24/7)


Your code snippet will always pass true. I think you were looking for something more like this:

Code
if((int)x[i] >= 48 && (int)x[i] <= 58)
{
  ret++;
}



Also, let me get nitpicky here for a sec and point out the things I would yell at the programmers working under me for.

In your numchecker function (and in any piece of code for that matter) always some whitespace between your variables and any kind of logic structure (whether it's your for loop or some other function call).

Always put whitespace between logic structures that aren't directly interacting with each other. i.e. You don't need white space after your beginning for loop line because the inner if statement is only being hit because something passes the for loop, but I would absolutely put white space between the end of the for loop and your if ret check.

These are little things that when it comes to letting team members maintain your code in the future add up to huge gains in productivity. (keep in mind the whitespace stuff is nitpicky and isn't a massive thing but it's more a show of experience. You'll almost never find a programmer with 10+ years of experience that doesn't use ample whitespace (but doesn't over use it) because they've had to go through code before that was all jammed together and know what a hassle it is.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 24 2012 01:59pm
could you give an example
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 24 2012 08:01pm
Quote (Thrasher66099 @ Sep 24 2012 11:41am)


yea you are correct i meant && instead of ||
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll