Hướng dẫn how to move a character in a string in c++ - cách di chuyển một ký tự trong chuỗi trong c++

Tôi phải di chuyển một ký tự tại một thời điểm trong đầu vào chuỗi của người dùng. Tại mỗi lần lặp, chương trình di chuyển các ký tự sang bên phải từ vị trí hiện tại của họ một cách tròn. Đó là nhân vật đầu tiên được chuyển đến vị trí thứ 2, ký tự thứ 2 được chuyển đến vị trí thứ 3, v.v. Nhân vật cuối cùng được chuyển đến vị trí thứ nhất. Sau khi các ký tự được di chuyển, chương trình cũng phải in chuỗi mới trong mỗi lần lặp. Các lần lặp được tiếp tục cho đến khi chuỗi ban đầu được quay lại.

Ví dụ: người dùng nhập chuỗi: 'Cat'

Sau khi chuỗi lặp đầu tiên là: 'TCA'

Sau chuỗi lặp thứ hai là: 'ATC'

và lần lặp thứ ba: 'Cat' (giống như và kết thúc chương trình)

Tôi đã viết một mã đảo ngược toàn bộ chuỗi. Nhưng, tôi thực sự không biết làm thế nào để di chuyển một nhân vật tại một thời điểm. Mã này như được hiển thị bên dưới:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

main ()
{
    char  word[20];
    printf("enter word:");
    int d,i;
    scanf("%s",&word);
    d = strlen(word);

    for(i=d-1; i >= 0; i--)
        printf("%c",word[i]);
}

#include <bits/stdc++.h>4std;1

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
2225555516

Đầu ra

Phân tích độ phức tạp:

Examples:  

Độ phức tạp về thời gian: O (n), vì chúng ta đang sử dụng một vòng lặp để đi qua n lần. Trong đó n là độ dài của chuỗi. !@$%^&*AJAY 
Output :AJAY!@$%^&*

Không gian phụ trợ: O (N), vì chúng ta đang sử dụng thêm không gian cho chuỗi Res1 và Res2. Trong đó n là độ dài của chuỗi.Geeksf!@orgeek@s A#$ c%o^mputer s****cience p#o@rtal fo@r ge%eks 
Output :Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%

Chúng ta có thể thay đổi một ký tự trong một chuỗi trong c không? Regular Expressions in Java

Để thay đổi một ký tự cụ thể trong một chuỗi thành một giá trị khác, chúng tôi đề cập đến vị trí số chỉ mục của ký tự trong chuỗi và sử dụng dấu ngoặc đơn ('' '). Để truy cập một ký tự của chuỗi trong C ++, chúng tôi chỉ cần chỉ định vị trí chỉ mục của ký tự trong chuỗi trong dấu ngoặc vuông [].

Implementation:

C++

#include <bits/stdc++.h>

C chấm dứt nhân vật nào?

string moveAllSC(string str)

{

Một chuỗi kiểu C là một mảng char null (ký hiệu là \ 0). NULL xảy ra sau ký tự cuối cùng của chuỗi. Để khởi tạo bằng cách sử dụng dấu ngoặc kép, "...", trình biên dịch sẽ chèn null.

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
2
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
4
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

Nhân vật đặc biệt nào sẽ được lưu trữ ở cuối chuỗi?

    {

Đầu của chuỗi được đánh dấu bằng một ký tự đặc biệt, ký tự null, đơn giản là ký tự có giá trị 0. (ký tự null không có quan hệ ngoại trừ tên của con trỏ null. được đặt tên là nul.)

Cải thiện bài viết

using4using5

#include <bits/stdc++.h>4using7

using4using9

    namespace1

Lưu bài viết

namespace1

Trong bài viết này, chúng tôi sẽ học cách di chuyển tất cả Char đặc biệt đến cuối chuỗi.

{

    std;0std;1std;2

    std;4std;5std;2

    std;8

    string moveAllSC(string str)0

Đầu vào:!@$%^&*Ajay & nbsp; đầu ra: ajay!@$%^&*

namespace1

Đầu vào: Geeksf! ***#@@%

Điều kiện tiên quyết: Biểu cảm thường xuyên trong Java

    string moveAllSC(string str)8 string moveAllSC(string str)9

    {

#include <bits/stdc++.h>4int #include <bits/stdc++.h>15

#include <bits/stdc++.h>4{6{7

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4    0

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
4
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4#include <bits/stdc++.h>17 #include <bits/stdc++.h>18#include <bits/stdc++.h>19 #include <bits/stdc++.h>20{77std;2

#include <bits/stdc++.h>4    0

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
4
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
8
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
9int #include <bits/stdc++.h>1

int8int9

using4using7

int8

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
03

#include <bits/stdc++.h>4namespace1

#include <bits/stdc++.h>4namespace3 namespace4

    namespace1

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
12 string moveAllSC(string str)8
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
14
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
15

    {

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
19std;1

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
21
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
22std;5
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
26

    namespace1

namespace1

Python3

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
30
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
31

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
33
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
35
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
36

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
38
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
40
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
41
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
40

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
8
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
45
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
46
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
47
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
9int0
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
50

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
52
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
54

#include <bits/stdc++.h>4#include <bits/stdc++.h>8

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
57
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
58
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
52
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34__

using4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
65
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
65
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
68
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
69

#include <bits/stdc++.h>4using7

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
63

using4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
74
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
74
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
68
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
69

    namespace3

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
65
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
68
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
83

#include <bits/stdc++.h>8

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
85
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
88
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
63

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
91
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
34 std;1
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
94

using4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
68std;5

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
99#include <bits/stdc++.h>00

C#

using #include <bits/stdc++.h>02

using #include <bits/stdc++.h>04

string moveAllSC(string str)5 #include <bits/stdc++.h>06

{

    string moveAllSC(string str)8 string moveAllSC(string str)9

    {

#include <bits/stdc++.h>4int #include <bits/stdc++.h>15

#include <bits/stdc++.h>4#include <bits/stdc++.h>17 #include <bits/stdc++.h>18#include <bits/stdc++.h>19 #include <bits/stdc++.h>20{77std;2

#include <bits/stdc++.h>4    0

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
4
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
8
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
9int #include <bits/stdc++.h>1

#include <bits/stdc++.h>4{

using4#include <bits/stdc++.h>5 #include <bits/stdc++.h>38

using4#include <bits/stdc++.h>8 #include <bits/stdc++.h>41

#include <bits/stdc++.h>42int9

using4using7

#include <bits/stdc++.h>42

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
03

#include <bits/stdc++.h>4namespace1

#include <bits/stdc++.h>4namespace3 namespace4

    namespace1

    

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
12 string moveAllSC(string str)8
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
14 #include <bits/stdc++.h>59

    {

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
19std;1
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
68

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
21std;5
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4#include <bits/stdc++.h>70

    namespace1

namespace1

JavaScript

#include <bits/stdc++.h>74

#include <bits/stdc++.h>75#include <bits/stdc++.h>76 #include <bits/stdc++.h>77

#include <bits/stdc++.h>4#include <bits/stdc++.h>17 #include <bits/stdc++.h>80

#include <bits/stdc++.h>4#include <bits/stdc++.h>17 #include <bits/stdc++.h>18#include <bits/stdc++.h>19 #include <bits/stdc++.h>85{77std;2

#include <bits/stdc++.h>4#include <bits/stdc++.h>17 #include <bits/stdc++.h>90

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3#include <bits/stdc++.h>92

#include <bits/stdc++.h>93#include <bits/stdc++.h>94

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
3
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
6

#include <bits/stdc++.h>4

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
8
Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
9#include <bits/stdc++.h>17 using01

#include <bits/stdc++.h>93#include <bits/stdc++.h>17 using04

#include <bits/stdc++.h>93#include <bits/stdc++.h>8 using07

#include <bits/stdc++.h>93using7

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
03

#include <bits/stdc++.h>4namespace1

#include <bits/stdc++.h>4namespace3 namespace4

#include <bits/stdc++.h>75namespace1

#include <bits/stdc++.h>75#include <bits/stdc++.h>17 using20

#include <bits/stdc++.h>4std;1

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%
2225555516

#include <bits/stdc++.h>75using27

    using29

Đầu ra

Geeksforgeeks A computer science portal for geeks!@@#$%^****#@@%

Phân tích độ phức tạp:

  • Độ phức tạp về thời gian: O (n), vì chúng ta đang sử dụng một vòng lặp để đi qua n lần. Trong đó n là độ dài của chuỗi., as we are using a loop to traverse N times. Where N is the length of the string.
  • Không gian phụ trợ: O (N), vì chúng ta đang sử dụng thêm không gian cho chuỗi Res1 và Res2. Trong đó n là độ dài của chuỗi., as we are using extra space for the strings res1 and res2. Where N is the length of the string.

Chúng ta có thể thay đổi một ký tự trong một chuỗi trong c không?

Để thay đổi một ký tự cụ thể trong một chuỗi thành một giá trị khác, chúng tôi đề cập đến vị trí số chỉ mục của ký tự trong chuỗi và sử dụng dấu ngoặc đơn ('' ').Để truy cập một ký tự của chuỗi trong C ++, chúng tôi chỉ cần chỉ định vị trí chỉ mục của ký tự trong chuỗi trong dấu ngoặc vuông [].. To access a character of a string in C++, we simply specify the index position of the character in the string in square brackets [] .

C chấm dứt nhân vật nào?

Một chuỗi kiểu C là một mảng char null (ký hiệu là \ 0).NULL xảy ra sau ký tự cuối cùng của chuỗi.Để khởi tạo bằng cách sử dụng dấu ngoặc kép, "...", trình biên dịch sẽ chèn null.\0 ) terminated char array. The null occurs after the last character of the string. For an initialization using double quotes, "...", the compiler will insert the null .

Nhân vật đặc biệt nào sẽ được lưu trữ ở cuối chuỗi?

Đầu của chuỗi được đánh dấu bằng một ký tự đặc biệt, ký tự null, đơn giản là ký tự có giá trị 0. (ký tự null không có quan hệ ngoại trừ tên của con trỏ null.được đặt tên là nul.)null character , which is simply the character with the value 0. (The null character has no relation except in name to the null pointer . In the ASCII character set, the null character is named NUL.)