Learn Computer Science programming - C# and C++ Algorithms

 

Number of Characters

Calculate the number of characters in a given string. Example in C++ below. 
Code
numofcharacters.zip
File Size: 4208 kb
File Type: zip
Download File

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


int NumOfCharacters(int x)
{
int Size=0;
if (x < 0) // Add 1 for negative value
{
Size = 1; x *= -1;
}
while (x) // Divide X by 10 and increase size
{
x = x/10; Size++;
}
return Size;
}


void main()
{


int x;


wcout <<  endl << "*** Number of Characters - How many chars are in -32636  ***" << endl;
wcout << "*************************************************" << endl;
int myInt5 = NumOfCharacters (-32636);
cout << endl << myInt5 << endl;


cin >> x;
 }