Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 2 [3]

Author Topic: A question (or more) to the C++ programmers about pointers  (Read 3053 times)

Jetlaw

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #30 on: October 20, 2009, 06:23:45 pm »

I managed to solve my problem ^^

The following source code can take any

Code: [Select]
char* szArrayOfStuff = { "Cookies", "More Cookies", "Even More Cookies", "No Cookies" }
style sz arrays.

The first function can be fed any char* (szArrayOfStuff + n) individual string and tell you how many characters is in it.

The second function can take the entire char* szArrayOfStuff[] and will tell you the length of the longest character string.

Code: [Select]
// Takes a single sz and counts along it until it reaches a null
// character, at which point it returns the length of the string from start
// until null.
int getCharStringCurrent(char* szString[])
{
   // Initilise the pointer.
   char* pszString;
   // Set it to the first character of the string.
   pszString = *&szString[0];
   
   // Initilise variables used in the loops.
   int nNumber = 0;
   int nCurrentString = 0;
   
   // I don't like infinite loops that require breaks to end.
   bool nComplete = 0;
   
   // While the function isn't complete...   
   while(nComplete == 0)
   {
      // If character at pszString+nNumber is not null...
      if(*(pszString+nNumber) != '\0')
      {
         // Increment to look at the next character.
         nNumber++;
         // And increment the length of the string.
         nCurrentString++;
      }
      // Else if character at pszString + nNumber is null...
      else if(*(pszString+nNumber) == '\0')
      {
         // Function is complete, we can break the while loop.
         nComplete = 1;
      }
   }
   // Return the length of the string.
   return nCurrentString;
}

// Takes an array of sz and decrements through them counting the number of
// characters in each string. The character size of the largest string within
// the array is returned.
int getCharStringBiggest(char* szString[], int nStrings)
{
   // Initilise the pointer.
   char* pszString;
   // Set it to the first character of the last string.
   pszString = *&szString[nStrings-1];
   
   // Initilise variables used in loops.
   int nNumber = 0;
   int nBiggestString = 0;
   int nCurrentString = 0;
   
   // While there are still strings to look at...
   while(nStrings != 0)
   {
      // If the character is null...
      if(*(pszString+nNumber) == '\0')
      {
         // Reset to reading the first character.
         nNumber = 0;
         // Set new biggest string equal to current string if current is bigger.
         if(nCurrentString > nBiggestString)
         {
            nBiggestString = nCurrentString;
         }
         // Reset current string.
         nCurrentString = 0;
         
         // Decrement the number of strings...
         nStrings--;
         // And if they are still strings to read, decrement to the next one.
         if(nStrings != 0)
         {
            pszString = *&szString[nStrings-1];
         }
      }
      // Else if the character isn't null...
      else if(*(pszString+nNumber) != '\0')
      {
         // Increment size of current string...
         nCurrentString++;
         // And increment the character number.
         nNumber++;
      }
   }
   // Return the size of the biggest string.
   return nBiggestString;
}
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: A question (or more) to the C++ programmers about pointers
« Reply #31 on: October 25, 2009, 06:00:35 pm »

It's a bit late, but isn't there a library function that returns a string's length?
Logged
Eh?
Eh!

florian

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #32 on: October 25, 2009, 06:07:19 pm »

strlen should do that. It's in <cstring> or <string.h>.
Logged

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: A question (or more) to the C++ programmers about pointers
« Reply #33 on: October 26, 2009, 07:56:15 am »

people who use c++ would be better off using std::strings instead of c-strings.

In this case.  The class function that gets the lenght is <string name>.length().
Code: [Select]
#include <iostream>
#include <string> 
using namespace std;
string szWhatever = "This is awesome";

int main ()
{
cout<<"string contains: "<<szWhatever<<endl;
cout<<"it's length is: "<<szWhatever.length()<<endl;
}
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.
Pages: 1 2 [3]