Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,431 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,926 people online right now. Registration is fast and FREE... Join Now!




Help understanding a c-string code

 
Reply to this topicStart new topic

Help understanding a c-string code, Need reading a code

jingoria
18 Jan, 2008 - 05:51 PM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 41


My Contributions
I am posting a code. Please someone can explain me in simple language what the pointers declared in the code really means. In other words, next to the line of code, please comment out what that line is suppose to mean. You do not need work out the whole code but couple of initial lines so I can refer it for my assignment. Especially the first lines where the functions & pointers are declared.

here is the code:


CODE
#include <iostream>
#include <cstdlib>
using namespace std;

void countDigits( char *inputString );
void countUpper( char *inputString );
char *toLower( char *inputString );

int main( int argc, char *argv[]){
  
   char *word;
  
   if ( argc > 1 )
      word = argv[1];
   else{
      cout << "You give me nothing to do!" << endl;
      exit(1);
   }
   cout << "The word is " << word << "." << endl;

   countDigits( word );
   countUpper( word );
   char newWord[128];
   strcpy( newWord, toLower( word ) );
   cout << "\"" << word << "\" has been changed to \"" << newWord << "\"\n";

   char *word1 = "testing";
   char *word2 = "TESTING";
  
   if ( strcmp( word1, toLower(word2) ) == 0 )
      cout << word1 << " = " << word2 << endl;
      
   return 0;
}

char *toLower( char *inputString ){
   int i;
   char returnString[128];
  
   for ( i = 0; i < strlen( inputString ); i++)
     returnString[i] = static_cast<char>(tolower( inputString[i] ) );
   returnString[i]='\0';
  
   return returnString;
}

void countUpper( char *inputString ){
   int count(0);
   for (int i = 0; i < strlen( inputString ); i++)
      if ( isupper(inputString[i]) )
         count++;
   cout << inputString << " contains " << count << " uppercase characters." << endl;

   return;
}
void countDigits( char *inputString ){
   int count(0);
   for (int i = 0; i < strlen( inputString ); i++)
      if ( isdigit(inputString[i]) )
         count++;

   cout << inputString << " contains " << count << " digits." << endl;
  
   return;
}


Would really appreciate your help.
thankyou.


*mod edit - added code tags - please post source code using the code tags, like this: code.gif
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Help Understanding A C-string Code
18 Jan, 2008 - 08:48 PM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

I'll give a shot at a few lines.

CODE

char *toLower( char *inputString );


Function "toLower" takes one parameter. That one parameter is a C-String called "inputString". This function returns a C-String.

char* represents a C-Style String, which is an array of characters. The last character in that array is 0, or NULL, signifying the end of the C-String.

inputString can be thought of as a pointer to the address where this array of characters is stored. inputString points to the first element (element 0) of this array.


CODE

char* word;


Assign word to be a pointer to a C-String but do not yet set aside any memory for that C-String.


CODE

char *word1 = "testing";



Assign the "word1" to point to a C-Style String. Since "testing" is 7 letters, assign 8 bytes of storage to "word1" (one for each letter, plus one byte for the NULL terminator. "word1" points to the place where the first 't' in "testing" is stored.


Hopefully I haven't screwed up with any terms. Don't think I have. Any other lines in particular?


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 05:01AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month