need help with strings

Jan 13, 2012 at 2:22pm
i have to erase first small letter from text. can anybody check and repair my code ?


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


void erasefirstsmall(char t[])
{
int position=0;
int small;
97<=small<=122;

while( t[position]!=0 && t[position]!=small)
{
position++;
}

if( t[position]== mala )
{

delete &t[position], &t[position+1];

}

}

int main()
{
int n=0;
char text [n];
cout<<"introduce text"<<endl;
cin>> text;
erasefirstsmall (text);
cout<< text;
system ("PAUSE");
}
Jan 13, 2012 at 2:28pm
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
#include <iostream>
#include <string.h>
using namespace std;


void erasefirstsmall(char t[])
{
    int position=0;
    int small;
    97<=small<=122; //What is this doing here? It does nothing at all...

    while( t[position]!=0 && t[position]!=small)
    {
        position++;
    }

    if( t[position]== mala )
    {

        delete &t[position], &t[position+1]; //What is the point of this? You can't delete a single char like this
        //Also it only uses &t[position+1] due to the comma.

    }

}

int main()
{
    int n=0;
    char text [n]; //illegal
    cout<<"introduce text"<<endl;
    cin>> text; //Buffer overflow
    erasefirstsmall (text);
    cout<< text;
    system ("PAUSE");
} 

Only delete the first element pointed by new, or you will have big problems!
Last edited on Jan 13, 2012 at 2:29pm
Jan 13, 2012 at 2:29pm
That won't work for multiple reasons.

Here's a working approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;

void erasefirstsmall(string& t)
{
    string::iterator pos = find_if(t.begin(), t.end(), ::islower);
    t.erase(pos);
}

int main()
{
    cout << "introduce text\n";
    string text;
    getline(cin, text);
    erasefirstsmall(text);
    cout << text << '\n';
}
demo: http://ideone.com/rGRfL
Jan 13, 2012 at 2:37pm
97<=small<=122;

That is nonsensical (did you mean (97 <= small) && (small <= 122), and it doesn't even do anything. What exactly are you trying to do here? I see that you've used 97 and 122 as the numerical limits of lower case letters, but what's supposed to happen if small is less than 97 or greater than 122? It doesn't do anything.

What does "erase" mean? If the input is

EFsDFR
should the output be

EF DFR or EFDFR ?


1
2
3
4
int n=0;
char text [n];
cout<<"introduce text"<<endl;
cin>> text;

This code makes an array of char, of size zero. It can hold ZERO letters. Why would you make an array that can hold ZERO letters, and then try to put letters into it? This makes no sense.

char text [n];
This is a variable length array. They are forbidden in C++. Your arrays must be a fixed size.

You're coding in C++. Time to use proper C++ strings.

1
2
3
4
5
6
7
8
9
10
11
//userInput is a std::string holding the user's entered word
for (int i = 0; i< userInput.length(); ++i)
  {
    if(islower(userInput[i]))
      {
        userInput.erase(i,1);
        break;
      }
  }

Last edited on Jan 13, 2012 at 2:45pm
Topic archived. No new replies allowed.