Using the find() Function

★ Modify the program so that it then replaces every a, e, i , o, u w/ the letter z.
i.e.
John Smith -> Jzhn Smzth

I am going to use the find() and replace() string functions to locate and replace the characters. This is my first time doing this so I am trying to teach myself. I wrote a part of the find function but it underlines the period Name.findin red for some reason. Any ideas of why this happens? The error says expected a semicolon....btw
Thanks! Anyone's imput is highly extolled!

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
// Strings are your friends.cpp : Defines the entry point for the console application.
//

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

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	string firstname, lastname;
	size_t found;

	cout << "Enter your first name.\n";
	cin >> firstname;
	cout << "Enter your last name.\n";
	cin >> lastname;

	//String object that store another two string objects
	string Name = firstname + " " + lastname;

	cout << "The name is : " << Name << endl;

	size_t Name.find(char 'a', 'e', 'i' , 'o', 'u', size_t pos = 0);




	
	system("pause");
	return 0;
}
Last edited on
Try omitting the size_t at the beginning of the line.
Name.find(char 'a', 'e', 'i' , 'o', 'u', size_t pos = 0);

HTH,
Aceix.
When I do that, the char 'a' becomes underlined and it says "type name is not allowed" and "expected a ')'.... I am obviously not done with the function but I don't think it should be a problem
Last edited on
One question that would help my understanding: In the example, what is size_t found;? Can someone explain if (found!=string::npos) from the example?

It says that the "member value npos is returned" if whatever your finding is not found. How do they return those random values for npos?

http://www.cplusplus.com/reference/string/string/find/
No, npos is a constant value, -1.
Therefore, if (found!=string::npos) means if found is not equal to -1.

HTH,
Aceix.
Last edited on
First, find_first_of is the function you want to use.

http://www.cplusplus.com/reference/string/string/find_first_of/
http://www.cplusplus.com/reference/string/string/find/

char 'a', 'e', 'i' is nonsensical.

A 'sequence' of chars can be indicated with a string literal:

Name.find( "aeiou", pos )

size_t pos = 0 is also nonsensical in the context of a function call. Default parameters are supplied in function declarations or definitions, not in function calls.



But I never put in parameters into my function declaration/definition. Look at my code! Does that mean it starts at pos=0 by default?

By the way, the link http://www.cplusplus.com/reference/string/string/find_first_of/ says to use "find()" if your looking for multiple characters...
Last edited on
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
// Strings are your friends.cpp : Defines the entry point for the console application.
//

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


int _tmain(int argc, _TCHAR* argv[])
{
	std::string firstname, lastname;
	const char *vowels = "aeiou";

	std::cout << "Enter your first name: ";
	std::cin >> firstname;
	std::cout << "Enter your last name: ";
	std::cin >> lastname;

	//String object that store another two string objects
	std::string Name = firstname + " " + lastname;

	std::cout << "\nThe name is: " << Name << std::endl;

	std::string::size_type n = 0;

	while ( ( n = Name.find_first_of( vowels, n ) ) != std::string::npos )
	{
		Name.replace( n, 1, 1, 'z' );
		n++;
	}

	std::cout << "\nNow the name is: " << Name << std::endl;


	return 0;
}
Topic archived. No new replies allowed.