Read from array1 write from array2 - arrays

Just learning some basic Ruby concepts as a beginner. Not really looking for code as such, rather some fundamental principles behind the following question (obviously feel free to express yourself with code if you need to :0)
In a simple redact text exercise, a user enters some text, then enters the word to be redacted, I'm fine with this and can make it work a number of ways.
However...
to deal with the possibility the user could enter upper and/or lower case letters for either the text or redacted word, I would need to create variables .downcase! again no problem there. But what if once the program runs, you want to return the words to their original state?
I thought perhaps you would need to create an array for the original text, where each word has an index within the array, create a corresponding array with the lowercase letters and if a word is NOT redacted, then you would compare the index from the lowercase array and write the corresponding index from the original array... does this sound correct or am I over thinking it, is there an easier way?
Thanks for your help
puts " What is your message"
text1 = gets.chomp
text2 = text1.downcase
puts "What is your secret word"
redact = gets.chomp.downcase!
words = text2.split (" ")
words.each do |x|
if
x == redact
print "REDACTED" + " "
else
print x + " "
end
end
I've added my working code, you can see that I've separated text1 the original from text2 which isn't strictly necessary as it stands, but to maintain the original formatting

Your solution sounds like it could work and as a beginner it may be useful to write a complete solution like that. But don't forget that ruby can do a lot of fun stuff for you.
Lets say we take input into sentence and the string to redact is stored in redact.
We can do something as simple as this:
sentence.gsub(/#{redact}/i, "*" * redact.length)
gsub finds all occurrences of the first argument and replaces it with the second, returning a new string.
First notice that we are using the redacted string as a regular expression for the first arg and the i indicates that it should match case insensitive, as you wanted.
Now the second arg is simply a string of asterisks of equivalent length to the redacted string.
For example if we have the following:
sentence = 'this is My Sentence'
redact = 'my'
puts sentence.gsub(/#{redact}/i, "*" * redact.length)
The above method will print this is ** Sentence.
Just one extra thing to note: this regex will match all occurrences of the string. For example, if redact = 'is', the resulting sentence will be th** ** My Sentence. You can re-write the regex to avoid this if that's not the expected use case.

Related

Creating an array that only contains the letters from a string

So I'm trying to change the string \t\n into an array of all of word characters in the string. The array I want would look like this: ["t","n"].
So far I've tried:
input = " \t\n"
array = input.scan(/\w/)
I've tried this regular expression on this string on rubular and it matches with all of the word characters as I'd like it to.
However, when using input.scan(/\w) an empty array is returned.
Please forgive my ignorance as I'm still new to this, but why is this?
Here you go! You were really close.
input = " \t\n"
array = input.dump.scan(/\w/)
=> ["t", "n"]
The key is to use String#dump (see: https://ruby-doc.org/core-2.6.5/String.html#method-i-dump)
I am not familiar with ruby but you seem to be having string interpolation confusion.
Per https://www.ruby-forum.com/t/new-line-in-string/176797
input = " \t\n"
Gives you a string with a space, tab, and newline.
You probably want to use single quotes to literally get the string you wrote:
input = ' \t\n'
If you sorely want to stick with double-quotes then I believe this would work:
input = " \\t\\n"
You should read https://blog.appsignal.com/2016/12/21/ruby-magic-escaping-in-ruby.html to learn more about string interpolation in Ruby. I would link you to the official docs but my lack of ruby experience translates to a lack of official doc experience.
So like colleagues explain in comments, the letters which you have in "\t\n" string are not ordinary letters, only something called special characters so I am not sure but there is not easy way to take this characters from this string cause \t is like one character.
With normal string like tn you could do something like this
"tn".split("")
and that give you array which you want.
But on special characters like in the example. you could do something like this
a = "\t\n".split("")
a.map! do |e|
if e == "\t"
"t"
elsif e == "\n"
"n"
end
end
which give you, I believe, results which you want.

Array of strings with re

This answer suggested me a code for having an array of conditions for replacements. Now I want to have one more condition which cannot be coded in the same way.
kxyz
sxyz
pxyz
clxyz
bookabcd
lookabcd
cookabcd
packabcd
bank
lab
court
catch
This is the updated word list
import re
# List where first is pattern and second is replacement string
replacements = [("ing$", "xyz"), ("ed$", "abcd")]
with open("new_abcd.txt", "w") as new, open("abcd.txt") as original:
for word in original:
new_word = word
for pattern, replacement in replacements:
new_word = re.sub(pattern, replacement, word)
if new_word != word:
break
new.write(new_word)
Let's say I want to code a conditions for words like 'bank', 'lab', 'court', 'catch' that says add "x". One may wonder what is a pattern in these words. It's nothing but all of these words are consonant ending. I don't know the Python way of doing this, but I want something like if the word does not end in ("a" or "e" or "i" or "o" or "u") change it to something else. Can re handle this?
It is not hard to check with regex whether the word ends in a non-vowel, but in this case you would not replace but just if you have a match, add a letter to the word.
The regex which will check the non-existence of a vowel at the end of the given string is
[^aeiou]{1}$
Then in python I guess do something like
regexp = re.compile(r'[^aeiou]{1}$')
if regexp.search(word):
word += 'x'
# do more stuff
I would check thoroughly whether python built in ways to do this are not faster.

I need to print out all of the words in the file that begin with an uppercase character

i need to access a file and from that file i need to print out all the words that starts with a capital letter and also how many times the words have occurred. for example in the file there is a text "the Program should Display Files and also Files"
now the output should be:
Text
Program
Display
Files(2)//This word is written two times
enter image description here
while (!feof(..)) is normally not a good idea, instead write
while (fgets(readLine,sizeof(readLine), fpointer) != NULL)
{
}
it seems ptr is superfluous in this context, if you want to check for words in the line you should move it forward in the line?
alt. use instead the runtime function strtok:
for (ptr = strtok(readLine, " "); ptr != NULL; ptr = strtok(NULL, " ")
{
// now ptr will point to each word in the line, then you just check
// if the first character is upper case.
}
This sounds like a homework assignment, so I am not going to put the code here. I can give you the steps to take to have a general idea:
Open and read the file
Use something like strtok to split the lines into words
Loop over the words and check the first character (remember that a word is an array of chars. You can check if it's in range of 60 - 95 as based on the ascii table.
To count words you can create a hashmap in which you store the word with a count as value e.g. {word1: 1, word2: 2}
In the end you go over all the keys in the hashmap and print the key + count.

how to print formatted output on terminal using C?

I have written a code for a Library system in C. And I want to show the output in following manner on terminal on Linux. I tried with "\t" but the output gets disturbed when the string size varies. I want to print it in fixed manner no matter what string size comes.
I want to print output like below-
I tried to print this using "\t" but the format gets disturbed when the string length of book or author gets smaller or larger. Can somebody help me with this??
Print with fixed character size. Here it is 7,11 and 10 for columns. Refer this for more details this
printf("Column1 Column2 Column3\n");
printf("%7d%11s%10d\n", 100, "String1", 9348);
printf("%7d%11s%10d\n", 23, "String2", 214);
use printf like this :
printf("%-25s|\n", "a string");
printf("%-25s|\n", "another string");
(the - in %-25s is use to left-justifies your text)
not a linux user (hope we are talking about monospace output) but my experienceis that tab has usually configurable size so if you format for 6 character length and someone have 4 character tab the result will be bad. The safest is to use spaces. You can use formated output like:
printf("float number: 8.3%f",7.56);
But that is not always a good choice for example sometimes negative sign mess up things ...
I usually handle such formatting my self with use of string variables:
line = ""
item = "single unformated text value"
compute length of item
add missing spaces (before or after) to line or item
add item to line
loop #2 for all items
output line
loop #1 for all lines

How do you copy part of a string and print it out

I am pretty new at coding and trying to figure out on how to take a part of a string and print out that part in a function.
For example: Hello
result: llo
Try using
String str = "hello";
System.out.println(str.substring(2));
It will leave the first characters and print the rest.
Similary you can use the substring() function with the syntax
Substring(startIndex, endIndex) where start index is inclusive and end index is exclusive.

Resources