simplify the code

how to wtite it using a char array?
Q: " given a string. obtain another one by swapping the parts of the first one after the first entry of space"


*dont mind the comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <string>
#include <iostream>
using namespace std;
int main()
{
    string test{ "basketball is a popular sport" };
    size_t sp = test.find(' '); // Базовий беззнаковий цілочисельний тип мови
    string test1;
    if (sp != string::npos) 
// статичне значення постійного члена з максимально можливим значенням для елемента типу size_t
        test1 = test.substr(sp + 1) + " " + test.substr(0, sp);
 /* Повертає нещодавно побудований об'єкт рядка з його значенням, ініціалізованим до копії підрядка цього об'єкта*/
    cout << test<<'\n';
    cout << test1 << '\n';
Last edited on
here's some idea but its unfinished and has missing parts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char** argv) {
 char s[20]="qwert dfgh";
 char s2[20];
 char *c=strchr(s,' ');
 cout<<*(c+1);
 //....
 s[i]!+'\0'
 //....
 for(int i=0;i<strlen(s)-5;i++)
  s2[i+5]=s[i];
 return 0;
}

For C++17,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	const char s[] {"qwert dfgh zxcv"};
	char s2[sizeof(s)] {};

	if (const auto sp {strchr(s, ' ')}; sp != NULL) {
		strcpy(s2, sp + 1);
		strcat(s2, " ");
		strncat(s2, s, sp - s);
	}

	cout << s2 << '\n';
}



dfgh zxcv qwert

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
#include <iostream>
#include <cstring>

int main() {

    // given a string. obtain another one by swapping the parts of the first one after the first entry of space
    
    const char srce[] = "basketball is a popular sport" ;
    char dest[ sizeof(srce) ] {} ;

    // locate the first space in srce
    const char* fs = std::strchr( srce, ' ' ) ;
    if( fs != nullptr ) { // found space

        // copy everything after the space
        std::strcpy( dest, fs+1 ) ;

        // append a space
        std::strcat( dest, " " ) ;

        // append everything in srce before the space
        const std::size_t num_chars = fs - srce ; // number of characters before the space
        std::strncat( dest, srce, num_chars ) ;

        std::cout << '"' << dest << "\"\n" ;
    }
}


http://coliru.stacked-crooked.com/a/f39bb1565ad4e140
This here works with pointers, without the need of making copy of the string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string.h>

int main()
{
    const char s[] = "basketball is a popular sport";
    printf( "%s\n", s );

    const char * c = strchr( s, ' ' );

    // prints the last part
    printf( "%s ", c + 1 );

    // prints the first part
    for( const char * p = s;  p < c;  p++ )
         putc( *p, stdout );

    putc( '\n', stdout );
}

*edited
Last edited on
thank you all so much!! :)
Topic archived. No new replies allowed.