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.
Related
Is there a way to replace all occurrences of a word in a string in C with another word. By word, I don't mean substring.
Here's what I want to achieve:
Input String:
OneOne One One OneOneOne One
Word to find:
One
Word to Replace it with:
Forty
Desired Output:
OneOne Forty Forty OneOneOne Forty
there are many example functions for replacing words in a string, i.e. What is the function to replace string in C?
you can use these functions to do what you want because the (white)spaces are also characters, see Removing Spaces from a String in C? and https://www.cs.tut.fi/~jkorpela/chars/spaces.html
so in the replace functions it is a difference if the replace string is
replace ='One' or replace = ' One '
if you use the second this should work
https://en.wikipedia.org/wiki/Whitespace_character
Unicode stored in C char
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.
I am starting to learn C and have a question about arrays.
Here I have a 2-dimensional simple array of characters:
char memory[100][6];
...
// populate indexes
and here I try to print out the first row:
puts(memory[0]);
Here is the output. Why does every row print out?
00P00300P10404P1ZZZZ0000ZZ0010
I come from Java where if you run the same code you will only get the first row. Am I missing something or is this just how C handles arrays? If so, how can I just get the first row?
The puts function is strictly for C-style strings. If you pass it a pointer to something other than a C-style string, garbage is likely to result. How are you expecting it to know how many characters to output?
I have an Arduino project with a string, called string, which is four digits, each between 0 and 9. So for example, a possible value is 1200. I'd like to take the first character, 1, and assign it to another string, called xCo.
String string = String(c);
String xCo = String(string[0]);
Serial.print(xCo);
Strangely, the Serial.print(xCo); line doesn't just print the first character, 1. Rather, it prints the whole string. I've read other questions' answers and they said that to reference a particular character, you just choose the index number of that character by doing something like string[0]. Yet, this isn't working for me.
What am I doing wrong here?
Edit: As the commenters have pointed out, String is an Arduino type, at least I'm pretty sure. My C and Arduino experience is very limited, so I can't be sure.
If you need to get the value of a character at a given position in a string, use charAt().
String string = "1200";
char singleCharacter = string.charAt(0);
Serial.print(singleCharacter);
Lot of people recommends to not use String. The best way is to simply use char *
char *foo = "1200";
char c = foo[0];
I am parsing a text file, and when I come across the word .word, I want to grab the rest of the line. Here is what I have so far:
char *word_ptr;
if (strstr(token, ":")){
// Some code
}
else if ((word_ptr = strstr(token, ".word"))) {
char *string_wanted = word_ptr + 6;
printf("Rest: '%s'\n", string_wanted);
}
string_wanted is not printing correctly. Is my usage of word_ptr correct when assigning it in the else-if statement? string_wanted is printing out nothing. When I add a 7 instead of a 6, it prints out 'ize'. I had the word size in my text file but now I removed it, I deleted the file and re-created it and done a clean build and the word 'ize' still shows up!! It does not exist in the file at all anymore so where did it come from?? I am really frustrated the word 'size' does not exist anymore in the file.
Here is what the file looked (when I had the word 'ize'):
array: .word 0:10
array_size: .word 10
Now I just removed the second line, so it is:
array: .word 0:10
Why isn't word_ptr printing out 0:10 when I add a 6 to it? I am pretty sure that word_ptr points to .word because when I print it, it prints .word. When tokenizing the line, array: is being tokenized so I know that it is getting there.
Any suggestions?
Thanks for your help.
We're running around in circles a bit here. I think I see where your confusion is now, so I'll try to lay it out. Your tokenizer is taking a string and breaking it up into tokens. Each of these tokens is a separate string by itself. You don't specify what characters you are tokenizing on, so I'll just assume the space character.
In this case the string:
array: .word 0:10
becomes three new strings:
"array:"
".word"
"0:10"
If you are looping on your tokens (it appears that you are) then first time through the loop token will be "array:", the second time it will be ".word" and the third time it will be "0:10".
This evaluation:
word_ptr = strstr(token, ".word")
will only find ".word" during the iteration in which token contains ".word". When you then increment word_ptr by 6 chars you have moved past the end of token into undefined memory. Yes, "0:10" appears two characters after ".word" in your original string, but we are not looking at the original string in your call to strstr. We are looking just at token and token only contains ".word".
This is why it's failing. How to fix it depends somewhat on the rest of your implementation.
When you set *string_wanted to word_ptr + 6, you are pointing string_wanted at the null that terminates the string. So when you try to print *string_wanted you get an empty string. Try getting the next token instead.
Wouldn't your if (strstr(token, ":")) be true for all of your strings? It wouldn't ever get to the block of code you're asking about.