Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

How to separate a long sentence in char string [ ] into several lines ?

c++ char sting [ ] span several lines

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1 littlepotato

littlepotato

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 03 March 2013 - 06:58 PM

Hey guys,  I am writing program to count  the length of the string. Here is my code.  

 

 

#include<iostream>
 
using namespace std;
 
int stringLength(char string[]) {
    int i;
    for(i = 0; string[i] != NULL; i++);
    return i;
}
 
int numSpace(char string[]) {
    int c = 0;
    for(int i =0; string[i] != NULL; i++) {
        if( string[i] == ' ')
            c++;
    }
    return c;
}
 
int main(int argc, char** argv) {
    char string[] = "       ";
    char dummy;
    cout << stringLength(string) << endl;
    cout << numSpace(string) << endl;
    cin >> dummy;
 
    return 0;
}

 

For instance, the paragraph I want to count is  

Probability is a measure or estimation of how likely it is that something will happen or that a statement is true.

The higher the degree of
probability, the more likely the event is to happen, or, in a longer series of samples,

the greater the number of times such event is expected to happen.

 

When I put this paragraph in char string[] = "       "; the compiler ( I am using MS Visual C++ 2010) puts all the words in one line. 

 

My question is how to span several lines to make the code easier to read ?  

 

Appreciate for your time !

littlepotato 

 


Edited by dargueta, 03 March 2013 - 08:25 PM.


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4,554 posts

Posted 03 March 2013 - 08:30 PM

It's not the compiler, it's the rules of the language. You can insert line breaks into string constants using the escape sequence \n like so:

 

"This is a long string." will print as

 

This is a long string.

 

"This is a\nlong string." will print as

 

This is a

long string


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,397 posts

Posted 04 March 2013 - 12:26 AM

It's just like Dargueta said. In fact, we can extend that to the whole computing.

 

To be more specific, whatever information is just a string of bytes, the visual representations are just conventions, just like every editor takes the enter as a new line while in memory new line is just another byte in the chain.

 

Same with files of any kind, you are programing in a low level language so you get to see things the way they are, so it's up to you to do the translations to the visual world :D

 

same happens with tabs, even with speaker bell, go figure...



#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,067 posts

Posted 04 March 2013 - 11:12 PM

New C++11 standard added new kinds of string literals.


The roots of education are bitter, but the fruit is sweet.


#5 littlepotato

littlepotato

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 05 March 2013 - 07:22 AM

It's not the compiler, it's the rules of the language. You can insert line breaks into string constants using the escape sequence \n like so:

 

"This is a long string." will print as

 

This is a long string.

 

"This is a\nlong string." will print as

 

This is a

long string

 

Thank you ,  dargueta !

 

So if I want to give a prompt to ask if the user want to repeat the calculation, I should write

 

cout << "Do you want to repeat the calculation Y for Yes, N for no :  ";

 

To span this in two lines, i should write

 

cout << "Do you want to repeat the calculation \n

              Y for Yes, N for no :  ";

 

Is that correct ? 

 

Thanks a lot !



It's just like Dargueta said. In fact, we can extend that to the whole computing.

 

To be more specific, whatever information is just a string of bytes, the visual representations are just conventions, just like every editor takes the enter as a new line while in memory new line is just another byte in the chain.

 

Same with files of any kind, you are programing in a low level language so you get to see things the way they are, so it's up to you to do the translations to the visual world :D

 

same happens with tabs, even with speaker bell, go figure...

 

Hi Flying Dutchman, thank you for your reply. 

 

So can I write the following in the code ? 

 

char string[ ] = " Probability is a measure or estimation of how likely \n

                          it is that something will happen or that a statement is \n

                          true." ; 

 

Thank you ! 



#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,067 posts

Posted 05 March 2013 - 07:43 AM

You'll have to add \ at the end of line (just like with macros):

char string[ ] = " Probability is a measure or estimation of how likely \n \
                          it is that something will happen or that a statement is \n \
                          true." ; 

 

or use R prefix (note the parenthesis inside quotes):

char string[ ] = R"( Probability is a measure or estimation of how likely \n
                          it is that something will happen or that a statement is \n
                          true.)" ; 

The roots of education are bitter, but the fruit is sweet.


#7 littlepotato

littlepotato

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 05 March 2013 - 07:46 AM

Dargueta, sorry I made a mistake, it should be

 

cout << "\nDo you want to repeat the calculation"
             << " Y if Yes, N if No:   ";
 
if I want to separate in two lines. 
cout << "Do you want to repeat the calculation Y for Yes, N for no :  ";


You'll have to add \ at the end of line (just like with macros):

char string[ ] = " Probability is a measure or estimation of how likely \n \
                          it is that something will happen or that a statement is \n \
                          true." ; 

 

or use R prefix (note the parenthesis inside quotes):

char string[ ] = R"( Probability is a measure or estimation of how likely \n
                          it is that something will happen or that a statement is \n
                          true.)" ; 

 

Got it ! That's really helpful ! 

 

Appreciate it ! 



#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4,554 posts

Posted 05 March 2013 - 11:03 AM

I think there's some confusion as to whether you want to create line breaks in your source code to make it more readable to the programmer, and line breaks in the output to make it more readable to the user. FlyingDutchman's suggestions work for the former, though I believe the R prefix is a C++11 feature, right?


sudo rm -rf / && echo $'Sanitize your inputs!'


#9 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,067 posts

Posted 05 March 2013 - 11:52 AM

R prefix is indeed C++11 and is just one of many reasons to upgrade your compiler (if you haven't already)!


The roots of education are bitter, but the fruit is sweet.


#10 littlepotato

littlepotato

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 05 March 2013 - 04:31 PM

I think there's some confusion as to whether you want to create line breaks in your source code to make it more readable to the programmer, and line breaks in the output to make it more readable to the user. FlyingDutchman's suggestions work for the former, though I believe the R prefix is a C++11 feature, right?

My apologies, I didn't make it clear before.  I want to create line breaks in the source code rather than the output.  But I do have learnt from the first reply of yours in terms of how to create line breaks in the output ! Thank you !



R prefix is indeed C++11 and is just one of many reasons to upgrade your compiler (if you haven't already)!

Does it mean the R prefix method won't work on my MS Visual C++ 2010 ? I am updating it right now...



#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4,554 posts

Posted 05 March 2013 - 07:54 PM

According to this, it looks like even Visual C++ 2012 doesn't implement all of the C++11 features.


sudo rm -rf / && echo $'Sanitize your inputs!'


#12 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,067 posts

Posted 05 March 2013 - 11:52 PM

I've check with VS12 and R prefix doesn't work although this page states it should.


The roots of education are bitter, but the fruit is sweet.