C程序中字符数组的子集
我已经写了一个C程序来反转字符串的子集。我无法得到输出C程序中字符数组的子集,c,arrays,C,Arrays,我已经写了一个C程序来反转字符串的子集。我无法得到输出 #include<stdio.h> #include<conio.h> #include<string.h> main() { int i,j,n,k, size; char a[10]="aabbcc"; i=0; n=strlen(a); for(i=0;i<n;i++) { for(j=i;j<n;j++)
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,j,n,k, size;
char a[10]="aabbcc";
i=0;
n=strlen(a);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
for(k=i;k<j+1;k++)
{
printf("%c",a[k]);
size = strlen(a);
reverse(a[k], 0, size - 1);
printf("The string after reversing is: %s\n", a);
}
printf("\n");
}
}
getch();
}
void reverse(char str1[], int index, int size)
{
char temp;
temp = str1[index];
str1[index] = str1[size - index];
str1[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str1, index + 1, size);
}
#包括
#包括
#包括
main()
{
int i,j,n,k,大小;
字符a[10]=“aabbcc”;
i=0;
n=strlen(a);
对于(i=0;i首先,由于Barmar提到的语法错误,此代码将不会编译
第二,若你们只想打印所有的子字符串,那个么这应该是一个简单的任务
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,n,k, size;
char a[10]="aabbcc";
i=0;
n=strlen(a);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
printf("substring=");
for(k=i;k<j+1;k++)
{
printf("%c",a[k]);
}
printf("\n");
printf("reverse=");
// only reverse the loop!
for(k=j; k>=i;k--)
{
printf("%c",a[k]);
}
printf("\n");
}
}
}
#包括
#包括
int main()
{
int i,j,n,k,大小;
字符a[10]=“aabbcc”;
i=0;
n=strlen(a);
for(i=0;它甚至不应该在没有警告的情况下编译。reverse()
的第一个参数应该是数组,但是reverse(a[k],0,size-1)
将单个字符作为第一个参数传递。如果要处理子字符串,需要将其复制到临时数组中。否则,您正在修改原始字符串,将来的循环将无法恢复原始字符串。是!!但是如何传递当前子字符串以反转它。我应该做哪些更改才能使其工作?
#include<stdio.h>
#include<string.h>
void reverse(char str[], int index, int size); // you need to declare you function first
int main() {
int i,j,n,k, size;
char a[10]="aabbcc";
i=0;
n=strlen(a);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
char buffer[10];
int buffer_index = 0;
for(k=i;k<j+1;k++) {
buffer[buffer_index++] = a[k];
}
buffer[buffer_index] = 0; // add buffer terminating
printf("current substring=%s\n", buffer);
// reversing current substring
size = strlen(buffer);
reverse(buffer, 0, size - 1); // passing copy of substring instead of a
printf("The string after reversing is: %s\n", buffer);
printf("\n");
}
}
}
void reverse(char str[], int index, int size) {
char temp;
temp = str[index];
str[index] = str[size - index];
str[size - index] = temp;
if (index == size / 2)
{
return;
}
reverse(str, index + 1, size);
}