0

Question : Find the number of "\n"(new line character) present in any string.

Input : String s = "Shroud is\n a godlike player in\n video gaming community."

Output : 2

Here is my code :

    #include<bits/stdc++.h>
using namespace std;
int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int nseq=0;
   string s;
   cin>>s;
   for(int i=0;i<s.length()-1;i++)
   {
       if(s.substr(i,i+1)=="\n")
       {
           nseq++;
       }
   }
   cout<<nseq<<endl;
}
  • 1
    do you need to find line breaks or specifically the two characters `\n` ? – 463035818_is_not_a_number Apr 19 '21 at 12:07
  • @largest_prime_is_463035818 yes I need to find line breaks. – shyantan vullingala Apr 19 '21 at 12:08
  • Besides, escape sequences only appear in *programs*. The compiled string contains the actual newline character, not an escape sequence – Panagiotis Kanavos Apr 19 '21 at 12:10
  • @largest_prime_is_463035818 Isn't `\n` **one** character? – Adrian Mole Apr 19 '21 at 12:10
  • I'm new to coding. Can somebody post code as answer please – shyantan vullingala Apr 19 '21 at 12:10
  • 1
    Note that `if (s[i] == '\')` is not valid C++. The backslash starts an escape sequence, which will escape the following single quote, so your literal is missing its closing quote. – Adrian Mole Apr 19 '21 at 12:12
  • @AdrianMole [depends](https://godbolt.org/z/nKj38xh79). I am not sure if OP is asking the user to type `\n` or hit enter.... – 463035818_is_not_a_number Apr 19 '21 at 12:14
  • 2
    @shyantanvullingala Am I missing something here? Why not simply `std::cout << std::count(s.begin(), s.end(), '\n');`? Why all of that code to get the number of characters that match a certain character? Your code is also inefficient in that it calls `substr()` on every iteration, creating temporary `std::string` objects. This is why attempting to learn C++ from online competition websites is a waste of time. – PaulMcKenzie Apr 19 '21 at 12:19
  • @shyantanvullingala Also, your question states how to find the number of "escape sequences", but then within your post, you mention only `\n`. The newline is not the only escape sequence. What about tabs, form feeds, etc? Those are also escape sequences. And even for that, the solution is still going to be a single line of code using `std::count_if` instead of `std::count`. – PaulMcKenzie Apr 19 '21 at 12:34

3 Answers3

0

Scan the string until you find a '/'. Then check it the immediately following character is a 'n'. In both cases, continue the scan. Beware of the corner cases...

Warning:

This was written before the OP changed the question. With the new statement, this is even more elementary.

Yves Daoust
  • 48,767
  • 8
  • 39
  • 84
0

I suggest using std::count:

#include <algorithm> // std::count
#include <iostream>  // std::cin, std::cout
#include <string>    // std::string

int main() {
   std::string s = "Shroud is\n a godlike player in\n video gaming community.";
   std::cout << std::count(s.begin(), s.end(), '\n') << '\n';
}

Output

2

Unrelated suggestions:

Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
0

In C and C++, you write "string" to denote a literal string, and 'c' to denote a literal character. But not all things are valid between the quotes. How would you denote a string that has two lines? C++ says that the whole string needs to be on one line, so this is not valid:

"line one
line two"

Also how do you denote a string that contains quotes? This is also not valid:

"Bob says "hello"."

because the first strings ends at the double-quote -- that's two strings with some invalid stuff between them.

To get around these problems, the language allows escape sequences in strings or characters, which you can use to denote characters you can't type in there. You can write "line one\nline two", for example. The \n is NOT two characters, but just one. The compiler turns it into a line separator character when it interprets the string. Similarly, you can write '\n' do denote the single line separator character.

Matt Timmermans
  • 36,921
  • 2
  • 27
  • 59