示例
输入:char*input="abcd"
输出:char*output="bcde"
输入:char*input="abbbcd"
输出:char*output="bcdcde"
#include<stdio.h> #define N 80 void Convert(char *input, char *output) { int i, flag = 0; for(i = 0; input[i] != '\0'; i++) { output[i] = (input[i] - 'a' + 1) % 26 + 'a'; if(flag) output[i]++; if(input[i] == input[i+1] && flag == 0) flag = 1; else flag = 0; } output[i] = '\0'; } int main(void) { char input[N], output[N]; gets(input);//gcc中没有gets函数,改用fgets Convert(input, output); puts(output);//gcc中没有puts函数改用fputs }
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define N 255 struct word { char *str; int len; }; typedef struct word *myword; //根据字符串的长度进行排序 void mysort(myword w, int n) { int i, j; struct word tmp; for(i = 1; i < n; i++) { tmp = w[i]; for(j = i; j > 0 && w[j - 1].len < tmp.len; j--) w[j] = w[j - 1]; w[j] = tmp; } } void my_word(char *input, char *output) { int i,j,count, len, nword; char *p; struct word *pw, tmp; i = j = count = len = nword = 0; output[0] = '\0'; //去除无效字符,并以空格分割 do { if(isalpha(input[i])) count++; else { if(count > 1) { nword++; output[len++] = ' '; output[len] = '\0'; strncat(output, input + i - count, count); len += count; } count = 0; } }while(input[i++]); strcpy(input, output); if(nword) { //分割字符串,然后对每个单词进行处理 pw = (myword)malloc(sizeof(struct word) * nword); p = strtok(input, " "); while(p) { pw[j].str = p; pw[j++].len = strlen(p); *(p - 1) = '\0'; p = strtok(NULL, " "); } //对单词排序 mysort(pw, nword); //去除重复单词 for(i = nword - 1; i >= 0; i--) { for(j = i - 1; j >= 0; j--) if(pw[i].len != pw[j].len) break; else if(strcmp(pw[i].str,pw[j].str) == 0) { pw[i].str = NULL; break; } } //连接单词形成新的字符串 output[0] = '\0'; len = 0; strcat(output, pw[0].str); len += pw[0].len; for(i = 1; i < nword; i++) if(pw[i].str) { output[len++] = ' '; output[len] = '\0'; strcat(output, pw[i].str); len += pw[i].len; } free(pw); } else output[0] = '\0'; } int main(void) { char input[N], output[N]; fgets(input, N, stdin); my_word(input, output); fputs(output, stdout); return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> void MyDecrease(char *minuend, char* subtrahend, char *remainder) { int mlen, slen, rlen, mdot, sdot, rdot, i, borrow, result; //获取字符串长度,假设没有小数点 mdot = mlen = strlen(minuend); sdot = slen = strlen(subtrahend); //查找被减数中小数点位置 for(i = 0; i < mlen; i++) if(minuend[i] == '.') { mdot = i; break; } //去掉被减数中的小数点 if(mdot != mlen) { memmove(minuend + mdot, minuend + mdot + 1, mlen - mdot + 1); mlen--; } //查找减数中小数点的位置 for(i = 0; i < slen; i++) if(subtrahend[i] == '.') { sdot = i; break; } //去掉减数中的小数点 if(sdot != slen) { memmove(subtrahend + sdot, subtrahend + sdot + 1, slen - sdot + 1); slen--; } //对齐小数部分 result = (mlen-mdot) - (slen-sdot); if(result > 0) { memset(subtrahend + slen, '0', result); slen += result; subtrahend[slen] = '\0'; } else if (result < 0) { result = -result; memset(minuend + mlen, '0', result); mlen += result; minuend[mlen] = '\0'; } //对齐整数部分 if(mlen > slen) { memmove(subtrahend + mlen - slen, subtrahend, slen); memset(subtrahend, '0', mlen - slen); slen = mlen; } else if(mlen < slen) { memmove(minuend + slen - mlen, minuend, mlen); memset(minuend, '0', slen - mlen); mlen = slen; } rlen = mlen; //对齐之后相减 if(strcmp(minuend, subtrahend) > 0) { for(i = rlen - 1, borrow = 0; i >= 0; i--) { result = minuend[i] - subtrahend[i] - borrow; if(result < 0) { borrow = 1; result += 10; } else borrow = 0; remainder[i] = result + '0'; } remainder[rlen] = '\0'; } else if(strcmp(minuend, subtrahend) < 0) { for(i = rlen - 1, borrow = 0; i >= 0; i--) { result = subtrahend[i] - minuend[i] - borrow; if(result < 0) { borrow = 1; result += 10; } else borrow = 0; remainder[i] = result + '0'; } remainder[rlen] = '\0'; borrow = 1; } else { remainder[0] = '0'; remainder[1] = '\0'; return; } //添加小数点 rdot = mdot > sdot ? mdot : sdot; memmove(remainder + rdot + 1, remainder + rdot, rlen - rdot + 1); remainder[rdot] = '.'; rlen++; //去掉字符串后面多余的0 for(i = rlen - 1; i >= 0; i--) if(remainder[i] != '0') break; if(remainder[i] == '.') i--; remainder[i+1] = '\0'; rlen = i + 1; //去掉字符串前面多余的0 result = 0; for(i = 0; i < rlen; i++) { if(remainder[i] == '0' && remainder[i+1] != '.') result++; else break; } rlen -= result; memmove(remainder, remainder + result, rlen + 1); //添加负号 if(borrow) { memmove(remainder + 1, remainder, rlen + 1); remainder[0] = '-'; } } int main(void) { char str1[20], str2[20], output[20]; double a, b; //随机数测试 srand(time(NULL)); a = (rand() % 65535) / (double)(rand() % 1000 + 1); b = (rand() % 65535) / (double)(rand() % 50 + 1); printf("%g - %g = %g\n",a,b, a-b); sprintf(str1, "%g", a); sprintf(str2, "%g", b); MyDecrease(str1, str2, output); puts(output); return 0; }