0

how can i parse an input string in JavaScript ? ,and by parsing I mean parsing of a string character by character. i don't want to use any inbuilt methods. use of for loop will be helpful. i have tried a palindrome function on an integer number by using modulus of a number but not sure how to do this in a string

    enter code here
var str = "the quick brown fox jumps over a lazy dog";
 for (i=0; i<str.length;i++){
//the code goes here//
}

help me with a function which can be used in different programs to parse strings. i am new to programming and stackoverflow . i'll try to get better with time

  • what do you want to do with the string ? – marvel308 Oct 03 '17 at 10:31
  • actually i want to create a palindrome function which first parse the whole string and then reverses it . i am familliar with the javascript reverse method but if i be able to do without it that would be great – Varun Rajvanshi Oct 03 '17 at 10:33
  • You could use the index of the for loop to access characters in the string one at a time and do whatever you want with it. Like `var character = str.split('')[i];` – kharhys Oct 03 '17 at 10:33

1 Answers1

1

I mean parsing of a string character by character.

I am guessing you're wanting to go through the characters. You can do the following

var str = "the quick brown fox jumps over a lazy dog";
for (i=0; i<str.length;i++){
    var letter = str[i] // use this to implement your palindrome logic
    console.log(letter); // each letter will be printed individually
}

Think of strings as an array of characters. So you'd index your way out similar to how you'd do with an array.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95