如何用c语言将字符串字符从结尾复制到开头

时间:2018-01-08 13:36:29

标签: c string function

{ "aaa", "bb", "cccc", "d" }我希望这个字符串是这样的 {"cccc", "d", "aaa", "bb" }。我试图切换索引,它也没有工作。这可能是什么原因?

char *str;
str = (char*)malloc(sizeof(char)*N);
int r;
printf("please enter the number of the rotations");
scanf("%d", &r);
rewind(stdin);
gets(str);
rotate(str, strlen(str), r);///the function is a void func and 
                            ///I want it to change the indexes in the main string 
puts(str);

2 个答案:

答案 0 :(得分:3)

单个 C string 的形式为:

<div class="messages">
            <ul>

                    <li class="error">
                        ::before 
                        Your username and password didn't match. Please try again.
                    </li>                  
            </ul>
        </div>

但你所展示的是什么:

{ "aaa, bb, cccc, d" } //single, comma delimited C string

描述了4个字符串的集合,每个字符串是由空字符终止的字符数组。此外:

{ "aaa", "bb", "cccc", "d" } //four C strings

创建一个只容纳一个字符串的容器。

但是你描述的场景暗示你需要变量  可以包含多个字符串,例如其中一个:

 char *str;
 str = (char*)malloc(sizeof(char)*N);

通过修改,以下内容可以适用于其他两种形式中的任何一种,但为简单起见,此插图使用 char **array //used to create pointers to pointers of space. requires dynamic allocation. char *array[4]; // creates pointers to an array of strings. requires no dynamic allocation in this application. char array[4][5] // creates a 2D array, each with space enough for the longest string. No dynamic allocation require. 形式。 (另请注意,由于您的帖子中缺少关于函数实际执行的操作的信息,因此旋转原型是即兴创作的。)

char *array[4];

答案 1 :(得分:1)

您的问题有点不清楚,所以我不确定您要找的是什么,但让我们试一试。

我假设您要输入格式为{ "str1", "str2", ..., "strN" }

的字符串

这是一个包含可被视为双引号子串的字符串。

然后你希望能够旋转这些双引号子串。

快速而肮脏的实现可能如下所示。

注意:代码几乎无错误检查 - 必须添加。但是,我省略了错误检查以缩短代码。

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

int getStrCount(char* str)
{
    int found = 0;
    int i = 0;
    while(str[i])
    {
        if (str[i] == '\"')
        {
            ++found;
        }
        ++i;
    }
    return found/2;
}

void getStrN(char* str, int N, int* start, int* end)
{
    int found = 0;
    int i = 0;
    *start = 0;
    *end = 0;
    while(str[i])
    {
        if (str[i] == '\"')
        {
            ++found;
            if ((found % 2) == 1) 
            {
                *start = i;
            }
            else
            {
                *end = i;
                if (found == 2*N) break;
            }
        }
        ++i;
    }
}

void rotate(char* str, int r)
{
    int start, end;
    int words = getStrCount(str);
    r = r % words;
    if (r == 0) return;
    char* result = malloc(1024);
    strcpy(result, "{ ");
    char* w[words];
    int i;
    for (i=0; i<words;++i)
    {
        w[i] = malloc(1024);
        getStrN(str, i+1, &start, &end);
        memcpy(w[i], &str[start], end-start+1);
        w[i][end-start+1] = '\0';
    }
    int j=words-r;
    for (i=0; i<words;++i)
    {
        if (i != 0) strcat(result, ", ");
        strcat(result, w[j]);
        ++j;
        if (j == words) j = 0;
    }
    strcat(result, " }");
    strcpy(str, result);
    free(result);
    for (i=0; i<words;++i)
    {
        free(w[i]);
    }
}

int main(void) {
    int r;
    int start, end;
    char str[1024];
    fgets(str, 1024, stdin);
    printf("%s", str);
    printf("Words: %d\n", getStrCount(str));
    scanf("%d", &r);
    printf("Rotate: %d\n", r);
    rotate(str, r);
    printf("%s\n", str);
    return 0;
}

INPUT:

{ "aaa", "bb", "cccc", "d" }
2

输出:

{ "aaa", "bb", "cccc", "d" }
Words: 4
Rotate: 2
{ "cccc", "d", "aaa", "bb" }