C语言输入一个字符串,输出字符串的所有组合
2018-01-25 19:26
459 查看
#include <stdio.h>
#include <string.h>
#define MAXSTRING 160
/*************************************
Author: Stat&陈
*************************************/
unsigned all = 0; /* 组合个数 */
void get_strings(void);
void print_string(char *s, int count);
void swap(char *tp, int c);
void swap2(char *pt, int d);
int main(void)
{
get_strings();
return 0;
}
void get_strings(void)
{
int i;
char array[MAXSTRING];
for(i = 0; i < MAXSTRING - 1; i++)
if((array[i] = getchar()) == '\n' || array[i] == ' ')
break;
array[i] = '\0';
i = strlen(array);
print_string(array, i);
}
void print_string(char *s, int count)
{
int i, j;
char temp[count+1];
for(i = 0; s[i] != '\0'; ){
strcpy(temp, s);
printf("\n%10s\n", temp);
all++;
for(j = 1; ; j++){
if(temp[j+1] == '\0')
j = 1;
swap2(temp, j);
if(strcmp(temp, s) == 0)
break;
else{
printf("%10s\n", temp);
all++;
}
}
i++;
swap(s, i);
}
printf("The number of combinations of character: %u\n", all-1);
}
void swap(char *tp, int c)
{
char temp;
temp = tp[c];
tp[c] = tp[0];
tp[0] = temp;
}
void swap2(char *pt, int b)
{
char temp;
if(pt[b+1] == '\0')
return;
temp = pt[b];
pt[b] = pt[b+1];
pt[b+1] = temp;
}
#include <string.h>
#define MAXSTRING 160
/*************************************
Author: Stat&陈
*************************************/
unsigned all = 0; /* 组合个数 */
void get_strings(void);
void print_string(char *s, int count);
void swap(char *tp, int c);
void swap2(char *pt, int d);
int main(void)
{
get_strings();
return 0;
}
void get_strings(void)
{
int i;
char array[MAXSTRING];
for(i = 0; i < MAXSTRING - 1; i++)
if((array[i] = getchar()) == '\n' || array[i] == ' ')
break;
array[i] = '\0';
i = strlen(array);
print_string(array, i);
}
void print_string(char *s, int count)
{
int i, j;
char temp[count+1];
for(i = 0; s[i] != '\0'; ){
strcpy(temp, s);
printf("\n%10s\n", temp);
all++;
for(j = 1; ; j++){
if(temp[j+1] == '\0')
j = 1;
swap2(temp, j);
if(strcmp(temp, s) == 0)
break;
else{
printf("%10s\n", temp);
all++;
}
}
i++;
swap(s, i);
}
printf("The number of combinations of character: %u\n", all-1);
}
void swap(char *tp, int c)
{
char temp;
temp = tp[c];
tp[c] = tp[0];
tp[0] = temp;
}
void swap2(char *pt, int b)
{
char temp;
if(pt[b+1] == '\0')
return;
temp = pt[b];
pt[b] = pt[b+1];
pt[b+1] = temp;
}
相关文章推荐
- 题目:输入一个字符串,输出该字符串中字符的所有组合
- 题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。
- 输入一个字符串,输出该字符串中字符的所有组合 C#【总结】
- 输入一个字符串,输出该字符串中字符的所有组合
- 输入一个字符串,输出该字符串中字符的所有组合
- 【编程题】输入一个字符串,输出该字符串中相邻字符的所有组合。
- C语言 字符串 编一个程序,输入一个字符串,将组成字符串的所有非英文字母的字符删除后输出。
- 输入一个字符串,输出该字符串中字符的所有组合
- 输入一个字符串,输出该字符串中字符的所有组合。(腾讯2014笔试附加题)
- Java练习题-输入一个字符串,输出该字符串中字符的所有组合(二)
- Java练习题-输入一个字符串,输出该字符串中字符的所有组合
- Swift - 排列组合之全排列 (输入一个字符串,输出该字符串包含的字符的所有组合)
- 输入一个字符串,输出该字符串中字符的所有组合
- 【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。
- C语言:编写一个程序统计输入字符串中,各个数字、空白字符、以及其他所有字符出现的次数。
- 输入一个字符串,删除其中所有的数字,所有大写字母改成小写,其他不变,并输出
- 编写函数fun(char *s),其功能是:把字符串中所有字符前移一个位置,串中的第1个字符移到最后。 例如,若输入字符串为:ABC123xyz ,则应输出字符串: BC123xyzA 。
- 题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。
- 输入字符串,输出字符串所有组合
- 如何输出一个字符串的所有组合