Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions searching/Search_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script>

// Javascript program to find lexicographically next
// string

function nextWord(s)
{
// If string is empty.
if (s == "")
return "a";

// Find first character from right
// which is not z.

var i = s.length - 1;
while (s[i] == 'z' && i >= 0)
i--;

// If all characters are 'z', append
// an 'a' at the end.
if (i == -1)
s = s + 'a';

// If there are some non-z characters
else
s[i] = String.fromCharCode(s[i].charCodeAt(0)+1);

return s.join('');
}

// Driver code
var str = "samez".split('');
document.write( nextWord(str));

// This code is contributed by noob2000.
</script>