How to become c++ string god

I am in the best high school in state.
I have first time met with programming (c++).
Iam 15 years old . I already learned basics(printf,scanf,if,while,for,do while)
I am learning strings at the moment.I really need help.Can you help me mastering it?
Sorry for horrible English.
c++ does not use printf, scanf, etc. it uses std:: string. These were used in original c++ before the STL (which came out in the mid or late 90s...). Its good to know them, as reams of older code still exists and uses it, but you should move on.

The thing you want to do is google c++ std::string and look at some simple code examples on how to use that.

Then start writing some small programs to exercise the tool and learn it.

There are TONS of examples and threads on std::string in this forum also.

We can help, but do some legwork first to get familiar with these tools.

Once you get string down, look up stringstream.

if you want to get good at C style coding strings, I can show you; I predate the stl and know that stuff in my sleep. But you are seriously wasting time learning that stuff too deeply; if you need to use/modify code that has it, or write C, you can learn it on demand at that time.



Last edited on
Sounds more like C than C++. Are you talking about "C-strings" (null-terminated char pointers) or C++ std::string?

Your question is really broad. Is there anything in particular you want to see an example of?

std::strings are pretty intuitive to use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example program
#include <iostream>
#include <string>

int main()
{
    std::string str = "Hello there";
    
    // Concatenate two strings
    str = str + ". How are you?";
    
    // Get the length of a string
    std::cout << "Length = " << str.length() << "\n";
    
    // Print a string
    std::cout << str << "\n";
    
    // Generate a substring from a string
    std::string str2 = str.substr(3, 5);  
    std::cout << str2 << std::endl;
    
    // Find if a substring is in a string
    size_t index = str.find("there");
    if (index != std::string::npos)
    {
        std::cout << "\"there\" found starting at index " << index << "\n";   
    }
    else
    {
        std::cout << "substring not found\n";   
    } 
}



Length = 25
Hello there. How are you?
lo th
"there" found starting at index 6
As noted already, a “string” is not the same thing in C and C++, and you appear to be studying C, not C++.


In C, a string is an array of characters.

    char s[13] = "Hello world!";

The number of characters used in the string varies: the string ends when the character value is zero. We call this zero-value character “the null terminator” or “the null character”. We could rewrite the above declaration using strict array syntax:

    char s[13] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', 0 };

In other words, c-strings are special because they have a built-in language literal syntax: stuff in double-quotes. But they are still just an array.


Be careful of the difference between mutable and const arrays. All string literals are const. Hence, the following are acceptable:

1
2
const char* s1 = "Hello";
char s2[] = "Hello";

The first (s1) is a pointer to the const array "Hello".
You cannot modify it.
s1 and "Hello" are the same thing.

The second (s2) is a mutable array (of six characters) that is initialized (copied) from the const array "Hello".
You can change any element of the array as you wish.
s2 is a different array from "Hello", located at different places in memory.
they just have the same element-wise values.


As usual with arrays, you must make sure that the array is large enough to hold all the characters you intend to use. And you can use the plethora of string handling functions in <string.h> to manipulate the contents of the array.

1
2
3
4
5
6
7
char s[50];

strcpy( s, "Hello" );
strcat( s, " " );
strcat( s, "World!" );
s[6] = tolower( s[6] );
printf( "%s\n", s );

Hope this helps.
www.cplusplus.com/reference/string/ - Lots of info on strings.

Also don't for get to #include <string> since it is a library.
Topic archived. No new replies allowed.