Copying from a random position Inside an array to another array

I have a char array that contains some value. I want to copy a value from this array from some random index to some other random index. How can i do this?

#include<iostream.h> using namespace std; int main() { char ar[100]; strcpy(ar,"string is strange"); cout << ar ; return 0; } 

Now the ar array contains a string that is weird. Suppose I want to create another char cp array in which I want to copy a value from an arbitrary position of the ar index from 7 to 10. Is there some kind of string function we can use? I know that we can use the strncpy function, but it copies from the starting index to the number of characters mentioned. Is there another function or an overloaded version of strncpy that will allow me to do the same?

+4
source share
6 answers

Do it like

 strncpy (dest, ar + 7, 2); 

basically

 strncpy (destination, source + start_index, number_of_chars); 
  The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-termi‐ nated. 

Therefore, you need to zero out the end of the line manually:

 dest[nos_of_chars] = '\0'; 

UPDATE

You can use something like this:

 char *make_substring (char *src, int start, int end) { int nos_of_chars = end - start + 1; char *dest; if (nos_of_chars < 0) { return NULL; } dest = malloc (sizeof (char) * (nos_of_chars + 1)); dest[nos_of_chars] = '\0'; strncpy (dest, src + start, nos_of_chars); return dest; } 

When you use C ++, do not use char strings for processing instead, using the string class.

+2
source

Your code is in C ++, so use STL - do not create an array of characters of a fixed size, use std :: string. This has a substr (pos, n) method.

so your code will look like this:

 std::string str; str = "string is not so strange"; cout << str << endl; std::string small; small = str.substr(7, 3); cout << small << endl; 

much easier than doing potentially dangerous pointer arithmetic using C api.

+2
source

To copy the characters n , starting at position p from string1 to string2 , you can use:

 strncpy(string2, string1 + p, n); 

If you are dealing with C ++ strings ( std::string ), you can use the substr member function.

 std::string string1 = "......"; std::string string2 = string1.substr(p, n); 
+1
source

C ++

What are you trying to achieve? "weird string" reminds me of spellcheck β†’ permutation

 #include <algorithm> #include <string> #include <iostream> int main() { std::string s = "string is strange"; std::sort(s.begin(), s.end()); while (std::next_permutation(s.begin(), s.end())) std::cout << s << "\n"; return 0; } 

To really trade random positions: http://ideone.com/IzDAj

 #include <random> #include <string> #include <iostream> int main() { using namespace std; mt19937 random; string s = "string is strange"; uniform_int_distribution<size_t> gen(0,s.size()-1); for (int i=0; i<20; i++) { swap(s[gen(random)], s[gen(random)]); cout << s << "\n"; } return 0; } 
+1
source

You can take the address of a specific place in the array using, for example, &ar[i] .

For example, if you added the following line before return

 cout << &ar[6] << '\n'; 

he will print

  is strange
0
source

you can use memcopy function

 void * memcpy ( void * destination, const void * source, size_t num ); 

in your example

 memcpy(cp,ar+p,sizeof(char)*n) 
0
source

Source: https://habr.com/ru/post/1395599/


All Articles