|
From: Jason H. <jas...@bt...> - 2001-10-03 13:12:05
|
Hi, I notice you have done three posts so I'll answer them all here as best
I can. I have written a small console app to demonstrate them all too. I
don't know why as I guess that you should be learning this for yourself, but
it was fun for me.
I tried to post this earlier, but it never got through, so this is my second
try.
First of all for a random number you can use rand(). Some compilers provide
a RANDOM() macro, but I don't know about MinGW. It is best to give rand() a
seed using srand(), otherwise you are likely to get the same numbers every
time you run the program. I would use some value you know won't be the same
each time you run a program i.e. the time. To use rand() you need to include
<stdlib.h>; or <cstdlib> if you want to be c++ standard compliant.
To get a single character from a string, either a C string or a C++
std::string, then just use the [] operator with the relevant index. i.e.
char my_string[] = "HELLO";
char c = my_string[4]; // c is now "L"
if you want you can write your own function to do it in the way you want.
I'm not going to do that for you.
As for the sorting of a string in the way you want, I've written an
algorithm that works by going through your string and picking out each of
the letter as it comes to them in order, searching for the same ones and
skipping spaces. Have a look at my code below. There may be a better
algorithm around to do this, but why spend loads of time doing that unless
you are working hundreds of thousands of strings.
Heres my demo, it has been briefly tested and I can't see any great problems
with it:
#include <cstdio> // all around useful header
#include <cstdlib> // the rand() fuction is in this header, so don't forget
it
#include <ctime> // for time()
#include <iostream> // for cout and cin
int main(int argc, char *argv[])
{
using namespace std; // allow implicit use of std namespace
// Random number generation
int num;
srand((unsigned) time(NULL)); // give the rand() function a seed
// I use the time because it's not likely to be the same each time
// the program is run. There may be a better way of doing this?
for(int i = 0; i < 10; ++i)
{
num = (rand() % 99) + 1; // A random number between 1 and 100.
cout << "A random number " << i + 1 << ": " << num << endl;
}
cout << endl;
// Getting a character out of a string
char a_string[] = "This is a string of text. Hello World!";
cout << a_string << endl << "Enter a number between 1 and "
<< sizeof(a_string) - 1 << ": ";
// the string is null terminated, so sizeof reports one bigger
// than text
cin >> num;
cout << "The character at that number is: " << a_string[num-1] << endl;
// You don't need a function to get a single character out of a
string
// just use array access. This applies to std::string too.
// don't forget... arrays are indexed from 0
cout << endl;
// Sorting a string in sequence
char str[80]; // up to 80 chars
cout << "Enter a string to be sorted in sequence (80 char MAX!)"
<< endl << ": ";
gets(str); // get a string from user (part of <cstdio>)
// Begin sort (brute force)
int i = 0, j = 0, k = 0; // index's
char sorted[80], c;
while(str[i])
{
while(str[i] == ' ') ++i; // skip spaces
if(!str[i]) break; // avoid end of line
j = i;
c = str[i];
while(str[j])
{
if(str[j] == c)
{
sorted[k] = c;
str[j] = ' '; // don't get the same char twice
++k; // increment sorted index
}
++j; // increment search index
}
++i; // increment input index
}
sorted[k] = 0; // don't forget to null terminate string!
cout << "Sorted string is: " << sorted << endl;
// NB: there may be a better way to do it, but this works and you
// don't really need anything faster for just 80 chars.
// I leave it to you to perform size and type checking anywhere
system("PAUSE");
return 0;
}
----- Original Message -----
From: "Mansoor Nathani" <man...@sy...>
To: <dev...@li...>
Sent: Wednesday, October 03, 2001 12:12 AM
Subject: [Dev-C++] Help with functions
> Does any one know the functions for :
>
> Generating a random number (integer) between a given range.
>
> Extracting a character from a string: eg:
>
> ( "HELLO", 4 )
>
> that should output the fourth character : L
>
> Any help would be appreciated
>
> Mansoor
>
>
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|