[C language] how to find index number (from 1) in new string. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[C language] how to find index number (from 1) in new string.

Example : First, I have a string = 'stayhome' Then, the new string is 'eomhyast' So, the index of new string (from 1) : e = 1, o = 2, m = 3, etc. I want the output is the index of first string, but use the sequence of the second string So, if the string is 'stayhome', then index s = 7, t = 8, a = 6, y = 5, etc. So, if I input : stayhome eomhyast Then, the output : 7 8 6 5 4 2 3 1

13th Jan 2021, 7:07 AM
Briana
Briana - avatar
8 Answers
+ 5
Are you supposed to read the inputs for the 2 strings, or are they hardcoded? I have the code ready, but I'd like to see your code first : ) Hint: Use strchr() from <string.h>
13th Jan 2021, 7:26 AM
Ipang
+ 5
Ipang that's interesting. How didn't I thought of strchar() ? I have used hashing instead.😅
13th Jan 2021, 7:30 AM
Arsenic
Arsenic - avatar
+ 5
Arsenic And I have no idea how hashing is used in C LOL 😁
13th Jan 2021, 7:32 AM
Ipang
+ 2
That's right NotAPythonNinja Duplicate letter or digit in either string (with same letter cases for alphabets) is indeed not suitable for strchr().
13th Jan 2021, 7:49 AM
Ipang
+ 2
Briana check this out 👇 https://code.sololearn.com/cBZam3lep7Eu/?ref=app But as NotAPythonNinja and Ipang said, things will get different when duplicates and different case characters come into play. For now this only works with lower case English alphabets [a-z].
13th Jan 2021, 10:18 AM
Arsenic
Arsenic - avatar
+ 1
Ipang how to find all index with strchr? I only know how to find position of a certain alphabeth
13th Jan 2021, 8:28 AM
Briana
Briana - avatar
+ 1
Ipang here is a way to get all indexes of a char in a str. You could use strchr() as well if you want to. https://code.sololearn.com/ca12385A24A2/?ref=app // no error check
13th Jan 2021, 8:43 AM
Flash
+ 1
Briana Where is your code? I've been waiting ... Use pointer arithmetic to get distance of a certain char from the string beginning. From there add 1 to the distance, because you wanted base 1 index. I have commented the code, and made adjustments to allow duplicate letters. https://code.sololearn.com/cNpCyPg4h0r3/?ref=app
13th Jan 2021, 2:08 PM
Ipang