blackstar (30)
Just needing pointers on figuring this out
|
|
I see substr takes (position, size)
so I'm assuming there is no way to actually put into substr to take out certain values but will have to write the string a certain way to equal the same
my current bones code looks like
|
|
Last edited on
Ganado (6722)
You should be able to do this without pointers. [That was a joke.]
"The program then uses the function substr to remove all the vowels from the string."
Where are you defining this function "substr" that you're calling? Not the clearest instructions, but I assume it's talking about the member function of std::string called "substr", and not your own function.
http://www.cplusplus.com/reference/string/string/substr/
I don't actually see how using substr is useful here.
Your program must contain a function to remove all the vowels
I don't understand signatures of the functions you're using. You're not even passing anything into your check_vowels function. To me, the instructions mean that you should write a function that takes in a string, and returns another string with the vowels removed. Or the string should be passed by reference. But either way, you need to pass a string as a parameter. Just like how you pass char ch as a parameter to check_vowels, you need to pass the string into your "remove vowels" function.
|
|
Now, if you still need to incorporate substr, read the link I pasted above, and contrive some way to use it (personally I don't think it's helpful).
Last edited on
blackstar (30)
The coding you gave me passes the check
but for learning sake i wanna learn how to use the substr if possible.
reading the link above tryen to figure out how to use the substr to pull out the vowels it comes across
blackstar (30)
i did change the abc code you had though, its cleaner
From:
|
|
to:
|
|
Last edited on
Ganado (6722)
tolower(ch) isn't mutating the character passed in, unless that's a custom function you made. Try testing your program with capital vowels.
I honestly am not sure how I would use substr here. I don't know what the teacher is looking for. Maybe someone else has a better idea.
Last edited on
Handy Andy (5051)
Hello blackstar,
Before you can use "substr" to get what is not a vowel you need to the position of the vowels in the the string.
Take a look at http://www.cplusplus.com/reference/string/string/find_first_of/ the example at the bottom of the page should be helpful. Knowing the position of the vowels you can use "substr" to build a new string.
Hope that helps,
Andy
lastchance (6977)
Sadly, I can't think of any remotely sensible way to use std::string.substr() to remove all the vowels. Are you sure that you have read your assignment correctly?
|
|
Last edited on
blackstar (30)
yeah it wants us to use the function substr() to remove all the vowels
http://www.cplusplus.com/reference/string/string/substr/
a few comments here work, but trying to change them to use the substr seems to be a headache.
handy andy seems to have to closet result, find the positioning then remove it with the substr
blackstar (30)
|
|
will find and replace, now just got to find and remove
MikeStgt (466)
Take this example here http://www.cplusplus.com/reference/string/string/find_first_of/
and instead of '*' use as replace string '\0'. To abuse substr() for this task is feasible, a nice exercise.
nuderobmonkey (640)
|
|
lastchance (6977)
|
|
Last edited on
Handy Andy (5051)
Hello blackstar,
BTW do not edit your original post when you make changes. It just confuses people that see the first post and do not realize that you have changed it. It is better to make a new message to show your changes.
After rearranging the instructions:
InstructionsWrite a program that prompts the user to input a string.The program then uses the function substr to remove all the vowels from the string.For example, if str = "There", then after removing all the vowels, str = "Thr".After removing all the vowels, output the string.Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel.
Here is the confusing part. Does "function substr" mean a function that you write or the member function of the string class called "substr"?
Now that I have reread everything a couple of times I understand better.
The page I pointed you to was to see how "find_first_of" works. Not so much to copy it and try to use it.
Using the return value of "find_first_of" and "myString.substr()" you can create a new string with out the vowels.
I do believe that is the intent of the instructions.
Hope that helps,
Andy
blackstar (30)
Oh i totally understand Handy andy, i just liked how simple their post was and decided to try and modify it.
As for the edits, it was more editing to read better but not modify the original words to the original post.
my understanding of things works better when its simple and slowly adding in more complicated function.
I also liked mikeStgt post, the \0 worked beautifully and kept the original coding intact and simple. even though we weren't able to get the substr in
MikeStgt (126)
Take this example here http://www.cplusplus.com/reference/string/string/find_first_of/
and instead of '*' use as replace string '\0'. To abuse substr() for this task is feasible, a nice exercise.
still reading over the other post as well
and i want to thank you all for the coding ideas though future reference next to the coding if you can add (//information about the code and why) so i can understand why it has been done
Example
|
|
But once again thank you all for your help
closed account (z05DSL3A)
|
|
Last edited on
MikeStgt (466)
My idea how to abuse substr() for this task was, concatenate the substring before the vowels with the substring after it, skipping this way over the vovel in question. I coded it last nite but did not publish it. (Honestly, it was late and I was too lazy to debug find(), it did not work as I assumed from REXX.)
Topic archived. No new replies allowed.
FAQs
How do I remove a vowel from a string in CPP? ›
- Initialize the variables.
- Accept the input.
- Initialize for loop.
- Check and remove the vowels from the string.
- Store the string without vowels using another for loop.
- Terminate both for loop.
- Print the string without vowels.
- int check_vowel(char);
- int main() { ...
- printf("Enter a string to delete vowels\n"); gets(s);
- for (c = 0; s[c] != '\0'; c++) { ...
- t[d] = '\0';
- strcpy(s, t); // We are changing initial string. ...
- printf("String after deleting vowels: %s\n", s);
- return 0;
To remove all vowels from a string in JavaScript, call the replace() method on the string with this regular expression: /[aeiou]/gi , i.e., str. replace(/[aeiou]/gi, '') . replace() will return a new string where all the vowels in the original string have been replaced with an empty string.
How do you separate vowels and consonants in a string in C++? ›Use isalpha() declared in string. h header to detect alphabet from string. Use conditional operate to separate vowels from other letters. Other alphabets are consonants as per definition.
How to remove certain words from a string in C? ›- Take a string and its substring as input.
- Put each word of the input string into the rows of 2-D array.
- Search for the substring in the rows of 2-D array.
- When the substring is got, then override the current row with next row and so on upto the last row.
Remove Characters From a String Using the replace() Method. The String replace() method replaces a character with a new character. You can remove a character from a string by providing the character(s) to replace as the first argument and an empty string as the second argument.
What is the function to remove vowels from a string in C? ›- Initialize the variables.
- Accept the input.
- Initialize for loop.
- Check and delete the vowels.
- Store the string without vowels using another for loop.
- Terminate both for loop.
- Print the string without vowels.
- /* C program to remove all characters in a string except alphabets */
- #include<stdio.h>
- int main()
- {
- char str[150];
- int i, j;
- printf(“\nEnter a string : “);
- gets(str);
C library function - remove()
The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.
- Algorithm. START Step-1: Input the string Step-3: Check vowel presence, if found return TRUE Step-4: Copy it to another array Step-5: Increment the counter Step-6: Print END. ...
- Example. ...
- Output.
How do I remove certain letters in regex? ›
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
Why do we use getline in C++? ›The C++ getline() is an in-built function defined in the <string. h> header file that allows accepting and reading single and multiple line strings from the input stream. In C++, the cin object also allows input from the user, but not multi-word or multi-line input. That's where the getline() function comes in handy.
How does the getline function work in C++? ›getline (string) in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.
Is vowel function in C++? ›If both isLowercaseVowel and isUppercaseVowel is true , the character entered is a vowel, if not the character is a consonant. The isalpha() function checks whether the character entered is an alphabet or not. If it is not, it prints an error message.
How do I remove specific text from a string? ›- Select a range of cells where you want to remove a specific character.
- Press Ctrl + H to open the Find and Replace dialog.
- In the Find what box, type the character.
- Leave the Replace with box empty.
- Click Replace all.
Method 1: Remove a Word from a String Using substr() Method
The “substring()” method assists in removing a word from a string. It extracts the needed part of the string by passing the start and the end index of the substring. As a result, a substring between the start and the last index will be returned.
- Take String input from user and store it in a variable called “s”.
- After that use replaceAll() method.
- Write regex to replace character with whitespaces like this s. replaceAll(“[^a-zA-Z]”,””);.
- After that simply print the String after removing character except alphabet.
- Use the String. indexOf() method to get the index of the character.
- Use the String. substring() method to get the part of the string before the character.
You append the replace() method on a string . The replace() method accepts three arguments: character is a required argument and represents the specific character you want to remove from string . replacement is a required argument and represents the new string/character that will take the place of character .
How to check for vowels in a string in C? ›- set the count to 0.
- Loop through the string until it reaches a null character.
- Compare each character to the vowels a, e, I o, and u.
- If both are equal, increase the count by one.
- Print the count at the end.
How to separate letters in a string in C? ›
In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.
Which function counts vowels in string? ›It is the len() function which actually gives the count of the number of vowels in the string.
How to remove special characters and spaces from a string in C++? ›Remove spaces from std::string in C++
To remove this we will use the remove() function. With this remove() function it takes the beginning and end of the iterator, then takes the third argument that will be deleted from that iterator object.
To remove all special characters from a string, call the replace() method on the string, passing a whitelisting regex and an empty string as arguments, i.e., str. replace(/^a-zA-Z0-9 ]/g, '') . The replace() method will return a new string that doesn't contain any special characters.
How to remove non alphabet characters from string in C? ›- Initialize the variables.
- Accept the input.
- Initialize a for loop.
- Iterate each character through the loop.
- Remove non alphabetical characters.
- Terminate for loop.
- Print result.
When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.
How to use delete function in C? ›C remove() function
The remove() function is used to delete a given file, named by the pathname pointed to by path. Return value from remove() function: The remove() function returns 0 if it successfully deletes the file. A nonzero return value indicates an error.
The remove() function takes the following parameter: filename - pointer to the C-string containing the name of the file along with the path to delete.
What is it called when you remove the vowels? ›Disemvoweling, disemvowelling (see doubled L), or disemvowelment of a piece of alphabetic text is rewriting it with all the vowel letters elided. Disemvoweling is often used in band and company names.
How do you find a vowel in a string? ›To find the vowels in a given string, you need to compare every character in the given string with the vowel letters, which can be done through the charAt() and length() methods. charAt() : The charAt() function in Java is used to read characters at a particular index number.
How to remove everything after A character in A string using regex? ›
We can also call the string replace method with a regex to remove the part of the string after a given character. The /\?. */ regex matches everything from the question to the end of the string. Since we passed in an empty string as the 2nd argument, all of that will be replaced by an empty string.
How to remove part of A string in regex? ›To remove substrings by regex, you can use sub() in the re module. The following example uses the regular expression pattern \d+ , which matches a sequence of one or more numbers. 123 and 789 are replaced by the empty string '' and deleted.
How to use regex to replace string? ›- Press Ctrl+R to open the search and replace pane. ...
- Enter a search string in the top field and a replace string in the bottom field. ...
- When you search for a text string that contains special regex symbols, GoLand automatically escapes them with backlash \ in the search field.
The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.
How to remove empty string in regex? ›Remove all whitespaces using regex
To remove all spaces in a string, you simply search for any whitespace character, including a space, a tab, a carriage return, and a line feed, and replace them with an empty string ("").
If you really want to read input from files in your C++ program, consider using C++ file primitives instead of using getline.
What is the difference between get () and getline () function in C++? ›get() takes the input of whole line which includes end of line space repeating it will consume the next whole line but getline() is used to get a line from a file line by line.
Why do we use CIN ignore () in C++? ›The cin. ignore() function is used which is used to ignore or clear one or more characters from the input buffer.
What is the difference between Readline and Getline? ›readline is similar to getLine , but with rich edit functionality and history capability. readline will read a line from the terminal and return it, using prompt as a prompt. If prompt is the empty string, no prompt is issued. The line returned has the final newline removed, so only the text of the line remains.
How to use Getline () in C++ when there are blank lines in input? ›How to use getline() in C++ when there are blank lines in input? In C++, we use the getline() function to read lines from stream. It takes input until the enter button is pressed, or user given delimiter is given. Here we will see how to take the new line character as input using the getline() function.
Does Getline ignore whitespace? ›
std::getline() does not ignore any leading white-space / newline characters.
How do you check if a character is a vowel? ›If either lowercase_vowel or uppercase_vowel variable is 1 (true), the entered character is a vowel. However, if both lowercase_vowel and uppercase_vowel variables are 0, the entered character is a consonant.
How do you check if a vowel is present in a string in C++? ›- Accept the input.
- Initialize the count vowel variable.
- Initialize for loop and terminate it at the end of the string.
- Check and count the number of vowels in a string.
- Print total count of vowels.
Its function can be described as syllabic, they take form of the nucleus of a syllable. Each word must consist of minimal, on vowel. Vowels are acoustically louder and have a stronger, long lasting, sonorous effect.
How do I remove one element from a string in CPP? ›In C++ we can do this task very easily using erase() and remove() function. The remove function takes the starting and ending address of the string, and a character that will be removed.
How do I separate characters from a string in CPP? ›- Using Temporary String.
- Using stringstream API of C++
- Using strtok() Function.
- Using Custom split() Function.
- Using std::getline() Function.
- Using find(), substr() and erase() Functions.
Remove specific substring from string in C++
erase(ind,substring. length()); // erase function takes two parameter, the starting index in the string from where you want to erase characters and total no of characters you want to erase.
The replace() method is the most straightforward solution to use when you need to remove characters from a string. When using the translate() method to replace a character in a string, you need to create a character translation table, where translate() uses the contents of the table to replace the characters.
How do I remove a specific element from a string? ›Remove Specific Characters From the String
Using str.replace(), we can replace a specific character. If we want to remove that specific character, we can replace that character with an empty string. The str.replace() method will replace all occurrences of the specific character mentioned.
C++ String erase() This function removes the characters as specified, reducing its length by one.
How do you isolate a character from a string? ›
- charAt() To extract a single character from a String, you can refer directly to an individual character via the charAt() method. ...
- getChars() If you wish to extract over one character at a time, you'll be able to use the getChars() technique. ...
- getBytes() ...
- toCharArray()
There is no built-in split() function in C++ for splitting strings, but there are numerous ways to accomplish the same task, such as using the getline() function, strtok() function, find() and erase() functions, and so on.
How do you slice a string in C? ›In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.
How do I separate text in a string? ›You can split the data by using a common delimiter character. A delimiter character is usually a comma, tab, space, or semi-colon. This character separates each chunk of data within the text string. A big advantage of using a delimiter character is that it does not rely on fixed widths within the text.
How do you separate words in a string? ›The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
How to split each letter in a string in C? ›- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char string[] = "characters";
- //Displays individual characters from given string.
- printf("Individual characters from given string:\n");
- //Iterate through the string and display individual character.