求关于字符串的好题目

匠心零度
博客专家认证
2011-05-17 04:48:33
凡是有关于字符串的好题目,只有发出就给分!
凡是有关于字符串的好题目,只有发出就给分!
凡是有关于字符串的好题目,只有发出就给分!
...全文
275 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
匠心零度 2011-06-18
  • 打赏
  • 举报
回复
谢谢大家哦!
就想叫yoko 2011-05-17
  • 打赏
  • 举报
回复
库函数自己实现
wallen99 2011-05-17
  • 打赏
  • 举报
回复
strcopy memcopy
赵4老师 2011-05-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//递归式回溯法生成全排列
if (depth==m) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要产生全排列的字符串\n",argv[0]);
return;
}
Make_Map(argv[1]);
Find(0);
}

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 26
int comb[MAX_NUM];
int c1,c2;
void combination(int m, int n)
{
int i, j;
for (i = m; i >= n; i--){
comb[n] = i; /* 选择当前的“头”元素 */
if (n > 1){
/* 进入下一次更小的组合问题 */
combination(i - 1, n - 1);
}else{
/* 满了需要的组合数,输出 */
for (j = comb[0]; j > 0; j--){
printf("%c", 65+c1-comb[j]);
}
printf("\n");
}
}
return;
}
int main(int argc, char *argv[])
{
if (argc<3) {
printf("%s 组合下标 组合上标\n",argv[0]);
return 0;
}
c1=atoi(argv[1]);
if (c1<1||26<c1) {
printf("1<=组合下标<=26\n");
return 0;
}
c2=atoi(argv[2]);
if (c2<1||c1<c2) {
printf("1<=组合上标<=组合下标\n");
return 0;
}
comb[0]=c2;
combination(c1, c2); /* C(4, 2) */
return 0;
}
铁甲小宝 2011-05-17
  • 打赏
  • 举报
回复
实现C语言标准库,或者是linux下的像是wc,grep等使用程序,都是非常经典的程序啊
pathuang68 2011-05-17
  • 打赏
  • 举报
回复
字符串操作的确有很多有意思的题目
千杯不醉-sen 2011-05-17
  • 打赏
  • 举报
回复
三楼写了很多,可算法。。。不喜欢。。。
就比如:

int Funct( int n )
{
if(n==0 || n==1) return 1;
retrurn Funct(n-1) + Funct(n-2);
}



#!/bin/bash
num1=1
num2=1
count=1
while((count<=10))
do
echo -n "$num1 $num2 "
((num1+=num2))
((num2+=num1))
((count+=1))
done
echo
//算法就这,linux的,没改,直接发了。。。


#include <stdio.h>

#include <math.h>



void main()

{

int x, y, pri_num, oth_count,pri_count;

int n = 10;

y = 0;

while( y < 2 * n - 1)

{

pri_num = abs(y + 1 - n) + 1;

oth_count = n - pri_num;

pri_count = 2 * pri_num - 1 ;

x = 0;

while (x < 2 * n - 1)

{

printf("%5d", x < oth_count ? (n - x ) : (x < oth_count + pri_count) ? pri_num : (x + 2 - n));

x++;

}

y++;

printf("\n\n");

}

}


#include <stdio.h>



int half_area(int n)

{

return n * (n - 1) / 2 + n;

}

int pre_area(int x, int y, int n)

{

return (x + y < n) ? half_area(x + y) : (n * n - half_area(2 * (n - 1) - x - y + 1));

}

int pre_line(int x, int y, int n)

{

return ((x + y) % 2 == 0) ? ((x + y < n) ? x : (n - y - 1)) : ((x + y < n) ? y : (n - x - 1));

}



int main()

{

int n, x, y;

printf("Please input n:");

scanf("%d", &n);



y = 0;

while(y < n)

{

x = 0;

while(x < n)

{

printf("%5d", (pre_area(x, y, n) + pre_line(x, y, n) + 1));

x ++;

}

y ++;

printf("\n\n");

}

}


#include <stdio.h>

#define LEN 5

#define M 2*LEN-1

#define N 2*(LEN-1)



void main()

{

int i, j, k = 1, u, v;

for(i = 0; i < M; ++i)

{

for(j = 0; j < M; ++j)

{

u = ( u = i < j ? i : j ) < ( v = N - i < N - j ? N - i : N - j ) ? u : v;

printf("%5d", LEN - u);

}

printf("\n\n");

}

}

一楼的这个可以搞定吧

int_char(int n)

{

int i;

if ( (i=n/10)!=0)

{

int_char(i);

}

putchar(n%10+'0');

}
匠心零度 2011-05-17
  • 打赏
  • 举报
回复
在实际开发中,对字符串的处理是常见的编程任务,在题目即是要求程序对用户输入的串进行处理,具体规则如下:
(1)把每个单词的首字母变为大写;
(2)把数字与字母之间用下划线(_)分开;
(3)把单词中间有多个空格的调整为1个空格
假设:用户输入的串中只有小写字母,空格和数字,不含其他的字母或符号,每个单词间由1个或多个空格分隔。假设用户输入的串长度不超过200个字符。
匠心零度 2011-05-17
  • 打赏
  • 举报
回复
求解下面的题目
luciferisnotsatan 2011-05-17
  • 打赏
  • 举报
回复
反转,搜索,插入删除字符等等
千杯不醉-sen 2011-05-17
  • 打赏
  • 举报
回复
1楼的简单了点,全排列,递归。
bdmh 2011-05-17
  • 打赏
  • 举报
回复
太多了,倒序,分割,去掉某些字符,等等
HeartWasNot 2011-05-17
  • 打赏
  • 举报
回复
给定字符串A和B,输出A和B中的最大公共子串。
比如A="aocdfe" B="pmcdfa" 则输出"cdf"
*/
//Author: azhen
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char *commanstring(char shortstring[], char longstring[])
{
int i, j;

char *substring=malloc(256);

if(strstr(longstring, shortstring)!=NULL) //如果……,那么返回shortstring
return shortstring;

for(i=strlen(shortstring)-1;i>0; i--) //否则,开始循环计算
{
for(j=0; j<=strlen(shortstring)-i; j++){
memcpy(substring, &shortstring[j], i);
substring[i]='\0';
if(strstr(longstring, substring)!=NULL)
return substring;
}
}
return NULL;
}


main()
{
char *str1=malloc(256);
char *str2=malloc(256);
char *comman=NULL;

gets(str1);
gets(str2);

if(strlen(str1)>strlen(str2)) //将短的字符串放前面
comman=commanstring(str2, str1);
else
comman=commanstring(str1, str2);

printf("the longest comman string is: %s\n", comman);
}


11.写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于
str2返回1,若str1小于str2返回-1
int strcmp ( const char * src,const char * dst)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
++src;
++dst;
}
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
3,求1000!的未尾有几个0(用素数相乘的方法来做,如72=2*2*2*3*3);
求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3,
能被625整除的数的个数n4.
1000!末尾的零的个数=n1+n2+n3+n4;
#include<stdio.h>
#define NUM 1000

int find5(int num){
int ret=0;
while(num%5==0){
num/=5;
ret++;
}
return ret;
}
int main(){
int result=0;
int i;
for(i=5;i<=NUM;i+=5)
{
result+=find5(i);
}
printf(" the total zero number is %d\n",result);
return 0;
}




1. 有双向循环链表结点定义为:
struct node
{ int data;
struct node *front,*next;
};
有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除
BOOL DeteleNode(Node *pHeader, DataType Value)
{
if (pHeader == NULL) return;

BOOL bRet = FALSE;
Node *pNode = pHead;
while (pNode != NULL)
{
if (pNode->data == Value)
{
if (pNode->front == NULL)
{
pHeader = pNode->next;
pHeader->front = NULL;
}
else
{
if (pNode->next != NULL)
{
pNode->next->front = pNode->front;
}
pNode->front->next = pNode->next;
}

Node *pNextNode = pNode->next;
delete pNode;
pNode = pNextNode;

bRet = TRUE;
//不要break或return, 删除所有
}
else
{
pNode = pNode->next;
}
}

return bRet;
}

void DE(Node *pHeadA, Node *pHeadB)
{
if (pHeadA == NULL || pHeadB == NULL)
{
return;
}

Node *pNode = pHeadA;
while (pNode != NULL)
{
if (DeteleNode(pHeadB, pNode->data))
{
if (pNode->front == NULL)
{
pHeadA = pNode->next;
pHeadA->front = NULL;
}
else
{
pNode->front->next = pNode->next;
if (pNode->next != NULL)
{
pNode->next->front = pNode->front;
}
}
Node *pNextNode = pNode->next;
delete pNode;
pNode = pNextNode;
}
else
{
pNode = pNode->next;
}
}
}
2. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
int GetCommon(char *s1, char *s2, char **r1, char **r2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int maxlen = 0;

for(int i = 0; i < len1; i++)
{
for(int j = 0; j < len2; j++)
{
if(s1[i] == s2[j])
{
int as = i, bs = j, count = 1;
while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs])
count++;

if(count > maxlen)
{
maxlen = count;
*r1 = s1 + i;
*r2 = s2 + j;
}
}
}
}
3. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数

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


void test1(long num);
char* test2(long num) ;


void main()
{
long n;
printf("输入long型变量n:");
scanf("%ld", &n);

printf("%ld转换为 2进制为:", n); test1(n);
printf("%ld转换为16进制为:%s\n", n, test2(n));
// printf("%s\n", test2(16L));
}


void test1(long num)
{
long mask = 0x1<<(8*sizeof(long)-1);
int i = sizeof(long)*8;


while (i)
{
if (num & mask)
putchar('1');
else
putchar('0');
mask = mask >> 1;
i--;
}
printf("\n");
}




char* test2(long num) //10-->16
{
char* buffer = (char*)malloc(11);
buffer[0] = '0';
buffer[1] = 'x';
buffer[10] = '\0';

char* temp = buffer + 2;
for (int i=0; i < 8; i++) {
temp[i] = (char)(num<<4*i>>28);
temp[i] = (temp[i] >= 0 ? temp[i] : temp[i] + 16);
temp[i] = (temp[i] < 10 ? temp[i] + 48 : temp[i] + 55);
}
return buffer;
}



输入N, 打印 N*N 矩阵
比如 N = 3,打印:

1 2 3
8 9 4
7 6 5

N = 4,打印:

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
解答:
1 #define N 15
int s[N][N];
void main()
{
int k = 0, i = 0, j = 0;
int a = 1;
for( ; k < (N+1)/2; k++ )
{
while( j < N-k ) s[i][j++] = a++; i++; j--;
while( i < N-k ) s[i++][j] = a++; i--; j--;
while( j > k-1 ) s[i][j--] = a++; i--; j++;
while( i > k ) s[i--][j] = a++; i++; j++;
}
for( i = 0; i < N; i++ )
{
for( j = 0; j < N; j++ )
cout << s[i][j] << '\t';
cout << endl;
}
}
2 define MAX_N 100
int matrix[MAX_N][MAX_N];

/*
*(x,y):第一个元素的坐标
* start:第一个元素的值
* n:矩阵的大小
*/
void SetMatrix(int x, int y, int start, int n) {
int i, j;

if (n <= 0) //递归结束条件
return;
if (n == 1) { //矩阵大小为1时
matrix[x][y] = start;
return;
}
for (i = x; i < x + n-1; i++) //矩阵上部
matrix[y][i] = start++;

for (j = y; j < y + n-1; j++) //右部
matrix[j][x+n-1] = start++;

for (i = x+n-1; i > x; i--) //底部
matrix[y+n-1][i] = start++;

for (j = y+n-1; j > y; j--) //左部
matrix[j][x] = start++;

SetMatrix(x+1, y+1, start, n-2); //递归
}

void main() {
int i, j;
int n;

scanf("%d", &n);
SetMatrix(0, 0, 1, n);

//打印螺旋矩阵
for(i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
}


斐波拉契数列递归实现的方法如下:
int Funct( int n )
{
if(n==0) return 1;
if(n==1) return 1;
retrurn Funct(n-1) + Funct(n-2);
}
请问,如何不使用递归,来实现上述函数?
请教各位高手!
解答:int Funct( int n ) // n 为非负整数
{
int a=0;
int b=1;
int c;
if(n==0) c=1;
else if(n==1) c=1;
else for(int i=2;i<=n;i++) //应该n从2开始算起
{
c=a+b;
a=b;
b=c;
}
return c;
}
解答:
现在大多数系统都是将低字位放在前面,而结构体中位域的申明一般是先声明高位。
100 的二进制是 001 100 100
低位在前 高位在后
001----s3
100----s2
100----s1
所以结果应该是 1
如果先申明的在低位则:
001----s1
100----s2
100----s3
结果是 4
1、原题跟little-endian,big-endian没有关系
2、原题跟位域的存储空间分配有关,到底是从低字节分配还是从高字节分配,从Dev C++和VC7.1上看,都是从低字节开始分配,并且连续分配,中间不空,不像谭的书那样会留空位
3、原题跟编译器有关,编译器在未用堆栈空间的默认值分配上有所不同,Dev C++未用空间分配为
01110111b,VC7.1下为11001100b,所以在Dev C++下的结果为5,在VC7.1下为1。

注:PC一般采用little-endian,即高高低低,但在网络传输上,一般采用big-endian,即高低低高,华为是做网络的,所以可能考虑big-endian模式,这样输出结果可能为4



判断一个字符串是不是回文
int IsReverseStr(char *aStr)
{
int i,j;
int found=1;
if(aStr==NULL)
return -1;
j=strlen(aStr);
for(i=0;i<j/2;i++)
if(*(aStr+i)!=*(aStr+j-i-1))
{
found=0;
break;
}
return found;
}
Josephu 问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。

数组实现:
#include <stdio.h>
#include <malloc.h>
int Josephu(int n, int m)
{
int flag, i, j = 0;
int *arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; ++i)
arr[i] = 1;
for (i = 1; i < n; ++i)
{
flag = 0;
while (flag < m)
{
if (j == n)
j = 0;
if (arr[j])
++flag;
++j;
}
arr[j - 1] = 0;
printf("第%4d个出局的人是:%4d号\n", i, j);
}
free(arr);
return j;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
printf("最后胜利的是%d号!\n", Josephu(n, m));
system("pause");
return 0;
}
链表实现:
#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
int index;
struct Node *next;
}JosephuNode;
int Josephu(int n, int m)
{
int i, j;
JosephuNode *head, *tail;
head = tail = (JosephuNode *)malloc(sizeof(JosephuNode));
for (i = 1; i < n; ++i)
{
tail->index = i;
tail->next = (JosephuNode *)malloc(sizeof(JosephuNode));
tail = tail->next;
}
tail->index = i;
tail->next = head;

for (i = 1; tail != head; ++i)
{
for (j = 1; j < m; ++j)
{
tail = head;
head = head->next;
}
tail->next = head->next;
printf("第%4d个出局的人是:%4d号\n", i, head->index);
free(head);
head = tail->next;
}
i = head->index;
free(head);
return i;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
printf("最后胜利的是%d号!\n", Josephu(n, m));
system("pause");
return 0;
}

已知strcpy函数的原型是:
char * strcpy(char * strDest,const char * strSrc);
1.不调用库函数,实现strcpy函数。
2.解释为什么要返回char *。
解说:
1.strcpy的实现代码
char * strcpy(char * strDest,const char * strSrc)
{
if ((strDest==NULL)||(strSrc==NULL)) file://[/1]
throw "Invalid argument(s)"; //[2]
char * strDestCopy=strDest; file://[/3]
while ((*strDest++=*strSrc++)!='\0'); file://[/4]
return strDestCopy;
}
错误的做法:
[1]
(A)不检查指针的有效性,说明答题者不注重代码的健壮性。
(B)检查指针的有效性时使用((!strDest)||(!strSrc))或(!(strDest&&strSrc)),说明答题者对C语言中类型的隐式转换没有深刻认识。在本例中char *转换为bool即是类型隐式转换,这种功能虽然灵活,但更多的是导致出错概率增大和维护成本升高。所以C++专门增加了bool、true、false三个关键字以提供更安全的条件表达式。
(C)检查指针的有效性时使用((strDest==0)||(strSrc==0)),说明答题者不知道使用常量的好处。直接使用字面常量(如本例中的0)会减少程序的可维护性。0虽然简单,但程序中可能出现很多处对指针的检查,万一出现笔误,编译器不能发现,生成的程序内含逻辑错误,很难排除。而使用NULL代替0,如果出现拼写错误,编译器就会检查出来。
[2]
(A)return new string("Invalid argument(s)");,说明答题者根本不知道返回值的用途,并且他对内存泄漏也没有警惕心。从函数中返回函数体内分配的内存是十分危险的做法,他把释放内存的义务抛给不知情的调用者,绝大多数情况下,调用者不会释放内存,这导致内存泄漏。
(B)return 0;,说明答题者没有掌握异常机制。调用者有可能忘记检查返回值,调用者还可能无法检查返回值(见后面的链式表达式)。妄想让返回值肩负返回正确值和异常值的双重功能,其结果往往是两种功能都失效。应该以抛出异常来代替返回值,这样可以减轻调用者的负担、使错误不会被忽略、增强程序的可维护性。
[3]
(A)忘记保存原始的strDest值,说明答题者逻辑思维不严密。
[4]
(A)循环写成while (*strDest++=*strSrc++);,同[1](B)。
(B)循环写成while (*strSrc!='\0') *strDest++=*strSrc++;,说明答题者对边界条件的检查不力。循环体结束后,strDest字符串的末尾没有正确地加上'\0'。
liyalei66 2011-05-17
  • 打赏
  • 举报
回复
利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧