help with some crazy string

Apr 18, 2008 at 3:25pm
hi,

im kind of new in programing and im trying to make some function that take string and go over the string and "cout" to me the letters that apear in the string, (one time each letter)

for example: cin>> im running ==> cou>>i m r u n g

i wrote this function but somthing wierd happen to the string each time :


#include <iostream.h>
#include <string.h>

void checkLetter (char* sen);

void main()
{


char* ptr="tghy";

checkLetter(ptr);

}




void checkLetter (char* sen)
{
char temp[4];
int cnt=0;
char* Letters;

for (int l=0;l<4;l++)
{
temp[l]=*(sen+l);

for (int j=0;j<4;j++)
{
if (temp[l]==*(sen+j))
cnt++;
}

if (cnt>1)
{
l--;
break;
}
}

Letters=temp;
//return Letters;

cout<< Letters <<endl;
}

please help me with this problem

thanks
Apr 18, 2008 at 7:12pm
I think you might benefit by first writing some pseudo-code to solve the problem and then translating the pseudo-code to C++:

void checkLetter( const char* sen ) {
string letters_already_seen;
for-each-char-in( sen ) {
if( char-is-not-in( letters_already_seen ) ) {
output char;
add char to letters_already_seen;
}
}
}


Apr 18, 2008 at 7:20pm
Ok, first a couple of comments on your post:

The description of your objective was okay, but "somthing wierd happen to the string" is not a good description of the problem. If you're unsure how to describe what's happening, then just post the output of the code you have so far. Additionally, when you post code, use the insert code button below and put the code between the boxes that pop up. It makes it much easier to read. That brings me to legibility of your code. The complete lack of indentation makes it a real pain to try and read. Proper indentation will be a life-saver as you progress into programming. Consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//your indentation:
for (int l=0;l<4;l++)
{
temp[l]=*(sen+l);

for (int j=0;j<4;j++)
{
if (temp[l]==*(sen+j))
cnt++;
}

if (cnt>1)
{
l--;
break;
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//much easier to read:
for (int l=0;l<4;l++)
{
    temp[l]=*(sen+l);
    for (int j=0;j<4;j++)
    {
        if (temp[l]==*(sen+j))
             cnt++;
    }

    if (cnt>1)
    {
       l--;
       break;
    }
}


The last thing I want to suggest is that you start using the comments in your code. This will not only make your code clearer to other programmers, it will help you interpret your own code when you come back to it at a later date. Trust me, comments are invaluable.

Okay, now onto the solution to your problem:

The first thing I noted is that you included the string library, but then you went with c-style strings. The string library could be helpful for this function if you use it properly, but for the purposes of this answer I'll just stick to c-style strings, since that seems to be what you're more comfortable with. The problem with what you have above is that the letters are being inserted into the temp array and then you're comparing them to the string you just got them from. So when temp[0] is compared to the char pointed to by sen, it will always, be the same. Then, your incrementing a cnt variable, and when it reaches 2, the loop is exited. That's why you only get the first two charachters. I'm not really sure how to utilize anything you've done there, so I wrote the following (note how much easier it is to understand what's going on with the comments in it):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
using namespace std;

void checkLetter (char* sen);

int main()
{


      char* ptr="this is my string";
      checkLetter(ptr);
      cout<<endl;
      system ("pause");
}


void checkLetter (char* sen)
{
     vector<char>tempvector;
    //set up an array of bools in which each portion of the index corresponds to the ASCII value of each char.  This will be 
    //used to indicate whether a particular charachter has already been used. False indicates that it hasn't been used yet, true
    //indicates that it has.
    bool checkchar[256];
    //Initialize all of the bool values to false.
    for(int i=0;i<256;i++)
    {
            checkchar[i]=false;
    }
    //iterate over the sentence
    for(int l=0; *(sen+l) != '\0'; l++)
    {
            //check the checkchar array to see if the char has been used
            //if it hasn't:
            if(checkchar[*(sen+l)]==false)
            {
                //push the charachter into the vector
                tempvector.push_back(*(sen+l));
               //change the value of checkchar to mark it as used
               checkchar[*(sen+l)]=true;
            }
     }
    //Print the vector
    for(int x=0; x<tempvector.size(); x++)
    {
            cout<<tempvector[x];
    }
}


Note that I used a vector for the storage of the charachters from the string. That made more sense to me for a couple of reasons. The first is that it simply makes the function more generic. In combination with the change to the for loop to for(int l=0; *(sen+l) != '\0'; l++) it allows me to hand the checkLetter function a string of any length, rather than a fixed size that has to be pre-determined. The other problem with using an array is that you don't know before hand how many charachters you're going to pull out of a given string. For example, from the string 'tghyt', the program should take the first four charachters and store it for output. But if I didn't already know that, I'd have to make the size of my temp array equal to the size of the string I hand it, just to be sure I don't run outside of the array. Then when I go to print it, how do I know how many charachters I need to print? I'd have no way of knowing, so I'd have to print out the whole thing, and then I'd end up with whatever garbage was in the tail end of that array being printed on screen. A vector just simplifies the whole process.

I hope this helps. If you have any other questions, just post back.
Last edited on Apr 18, 2008 at 7:21pm
Apr 18, 2008 at 7:55pm
closed account (z05DSL3A)
@gzero

>>The complete lack of indentation makes it a real pain to try and read.

To be fair, the code could be properly indented, it just will not show as such without the code tags.
Apr 19, 2008 at 8:46am
wow!!

thank u guys for helping me, its my first time in this forum, and i think that is not my last..

Topic archived. No new replies allowed.