String problems c++

Pages: 12
I have these problems c++.
1) Write a function with 2 parameters x and y, x string, and y character. The function returns the number of occurrences of y in x.
2)The function receives twice as parameters. If the strings have different lengths, the program returns the empty string, otherwise it returns the string chaining the characters of the first with the second alternately. Example. Abcd efgh -> aebfcgdh

Many thanks to all!
Last edited on
So what's your best effort so far?

I mean, you should be able to manage the basics of
1
2
3
4
int function ( string x, char y ) {
  cout << x << " " << y << endl;
  return 0;
}


Then you go and read all about std::string and see if there is anything that can help you with finding things.

So is right for the first?
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
#include <iostream>
//#include <string>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;


string occurrencesxy (string x, char b)
{

    int dim=x.length();
    int conta=0;

    for (int i=0; i<dim; i++)
{
    if(x[i]==y)
{
    conta++;
}
}
  cout<< conta;


}
int main ()
{

    string x;
    cin>> x;
    char y;
    cin>> y;
    cout << occurrencesxy (x, y);

    return 0;
}

Last edited on
> The function returns the number of occurrences of y in x.
What do you understand by the meaning of "the number"?

> string occurrencesxy (string x, char b)
OK, I suppose you could return "10" rather than 10.
That's entirely up to you.

But you just fall off the end of the function.
> The function returns the number of occurrences of y in x
I mean how many occurrences of y (for example the letter "a") are present in x
For example if x is " word" and y is "o" so the result is 1.

Is very difficult for me the second problem....
So this doesn't seem like magic to you?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// int, because that's what the requirement said.
int occurrencesxy (string x, char b)
{

    int dim=x.length();
    int conta=0;

    for (int i=0; i<dim; i++)
{
    if(x[i]==y)
{
    conta++;
}
}
  return conta;  // you know, the number you've been busy incrementing


}

Ok but the second problem is hard for me.....
> If the strings have different lengths
You know how to get the length of a string, so presumably you can get the length of two strings.

Presumably at that point, you also know how to use an if statement.

> otherwise it returns the string chaining the characters of the first with the second alternately
You've already figured out x[i] as a means of indexing a string, why not try x1[i] and x2[i] ?
"otherwise it returns the string chaining the characters of the first with the second alternately"

Excuse can you write the code about this?
> Excuse can you write the code about this?
Are you here to learn, or just score free homework.

Because if you're after free homework, then you've already failed. All it does is maintain the illusion that you can code, when in reality you're just falling. Sooner or later (closed book exam, job interview, job appraisal), your lack of ACTUAL skill will be found out.

You have everything to at least make an attempt (it's called learning by doing). You're not learning just by looking at the words and thinking "I can't do this".

Just an honest attempt, that's all.
Try it.
So is right for the first?

After having applied salem c’s corrections, you also need to include <string>, because you use it, but you can delete <ctime>, <cmath> and <cstdlib>, because you don't use them.
(And you could improve the code indentation, I think.)

About the second exercise, just try to describe yourself the problem:
1) I need a function func_name();
"The function receives twice as parameters." <-- I can't understand this (twice means two times
https://en.oxforddictionaries.com/definition/twice
Should your function get two strings and two characters? I don't think so. I think the exercise says the function should get two strings):
2) example of partial function prototype: func_name(std::string first, std::string second)
3) What is the expected return value? "...returns the empty string, otherwise it returns the string..."

Once you have written the basic prototype, do get on:
4) Inside that function, the first step is to check if... ?
"If the strings have different lengths..."

After having solved that, do deal with the biggest issue:
"the string chaining the characters of the first with the second alternately"
(which is not that big, actually)

I suggest not trying to do everything at the first attempt; perhaps it is better to manage it step by step.
"The function receives twice as parameters."
I mean receive two parameters.
I can't do this....
"the string chaining the characters of the first with the second alternately"

ps
I haven't studied "func_" so I can't use this.
Last edited on
> I haven't studied "func_" so I can't use this.
That's not what Enoizat meant.

Any name will do, but the more descriptive, the better.
1
2
3
4
5
func_name(std::string first, std::string second)
splice_strings(std::string first, std::string second)
im_a_banana(std::string first, std::string second)
sup_dude(std::string first, std::string second)
tricky_code(std::string first, std::string second)


Make up your own name.
I repeat
""the string chaining the characters of the first with the second alternately""
I am not able to do this really.....
You're not even trying are you?
1
2
3
4
5
6
7
string alternate_character_merger ( string a, string b ) {
    string result = "";
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;

    return result;
}


Add ONE statement which moves you closer to the answer, can you do that?
For the second part, you need a function that takes two strings as arguments, and returns a string as the result. Since the function is to put the strings together like a zipper, I'll call it:
string zipper(string s1, string s2)

Inside zipper, the first thing you need to do is see if the sizes are the same. If they are not, then return an empty string. You can do this with an if statement. You get the size of a string with the size() method, so, the size of s1 is s1.size()

If the sizes are the same then you can zipper the strings together. Use a for loop that increments a variable (let's call it i) from 0 to the size of the strings. Then, each time through the loop:
add s1[i] to the result string
add s2[i] to the result string.

When you're done, return the result string.

Hope this helps,
Dave
Help!! I can't reach, i must do it for tomorrow morning...

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
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
using namespace std;
int ConcAltStringhe(string a,string b, string c)
// es. abcd e efgh aebfcgdh
{
    int dim=a.length();
    int dim1=b.length();
    int dim2=c.length();
    int i=0;
    int j=0;

    if(dim==dim1)
    {
        for(int i=0;i<dim;i++)
        {
            a[i]=c[j];
            b[i]=c[j+1];
            j++;

        }
    }
    return c;

    }
int main()
{
  srand(time(NULL));

  string c;
  string a;
  cin>>a;
  string b;
  cin>>b;
  cout<<ConcAltStringhe(a,b, c);



    return 0;
}
Last edited on
Line 6: ConcAltStringhe() should return a string, not an integer.
Line 6: remove parameter c. You don't need it. Also remove it in the call at line 37, delete it at line 32 and delete line 11.
Line 13: replace this with string result;. This will be the string that you'll return from the function. You'll create the content of the string inside the function. As an aside, I always name the return value of a function result. It makes code consistent.
Line 25: change this to return result;

Now all you have to do is replace lines 19-21 with code that will (1) add i'th character of a to result and (2) add the ith character of b to result.

The i'th character of a is a[i]. The i'th character of b is b[i].
You can add a character to result with +=. For example, to add 'M' to result, you'd say result += 'M'

That should be enough to get you through. I have to leave work now so I won't be able to offer more help tonight. Good luck. If you get stuck for more than an hour then post your code again and maybe someone else will be able to help.

Dave
I never used "string result".. I need another code.....
> I never used "string result".. I need another code.....
You had never used int result at some point in your recent past.
Why would returning a string all of a sudden be a huge mental block over returning an int?

You're very close to solving this yourself.
Just pay attention to dhayden's comment about using +=

Pages: 12