#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include "mystack.h"
#define MAX_LINE 256
#define ARRAY_SIZE 32
//======================================================
// 去掉单词间多余的空格
//======================================================
int Handleblank(char *strInput, char *strOutput)
{
char *p = strInput;
bool bIsBlank = false;
bool bIsWord = false;
int num = 0;
int i = 0;
int j = 0;
for (i = 0; i < strlen(strInput); i++)
{
if (strInput[i] == ' ') {
if (!bIsBlank) {
bIsBlank = true;
strOutput[j++] = strInput[i];
}
bIsWord = false;
} else {
bIsBlank = false;
strOutput[j++] = strInput[i];
if (!bIsWord) {
bIsWord = true;
num++;
}
}
}
strOutput[j] = '\0';
return num;
}
//======================================================
// 将字符串中的单词进行分离
//======================================================
int SeparateWords(char *strInput, char *strptr[])
{
assert(strInput != NULL);
assert(strptr != NULL);
char word[MAX_LINE] = {0};
int i = 0;
int j = 0;
int k = 0;
bool bIsWord = false;
while (strInput[i] != '\0')
{
if (isalnum(strInput[i])) {
bIsWord = true;
word[j++] = strInput[i];
}
else if (bIsWord) {
bIsWord = false;
word[j] = '\0';
strptr[k] = (char *)malloc(sizeof(char) * (j + 1));
strncpy(strptr[k++], word, j + 1);
memset(word, 0, j + 1);
j = 0;
}
i++;
}
if (j != 0) // 最后一个
{
word[j] = '\0';
strptr[k] = (char *)malloc(sizeof(char) * (j + 1));
strncpy(strptr[k++], word, j + 1);
}
return k;
}
//======================================================
// 将字符串中的单词进行分离
//======================================================
int SeparateWords2(char *strInput, char *strptr[])
{
assert(strInput != NULL);
assert(strptr != NULL);
char word[MAX_LINE] = {0};
int i = 0;
int j = 0;
int k = 0;
bool bIsWord = false;
do {
if (isalnum(strInput[i])) {
bIsWord = true;
word[j++] = strInput[i];
}
else if (bIsWord) {
bIsWord = false;
word[j] = '\0';
strptr[k] = (char *)malloc(sizeof(char) * (j + 1));
strncpy(strptr[k++], word, j + 1);
memset(word, 0, j + 1);
j = 0;
}
i++;
} while (strInput[i] != '\0');
if (j != 0) // 最后一个
{
word[j] = '\0';
strptr[k] = (char *)malloc(sizeof(char) * (j + 1));
strncpy(strptr[k++], word, j + 1);
}
return k;
}
<pre class="cpp" name="code">//========================================================
// 该函数将输入的数值颠倒输出且不含相同数字
// 输入:82473413; 输出:314728
//========================================================
unsigned int HandleValue(unsigned int val)
{
char strInt[10] = {0};
char strInput[10] = {0};
char strOutput[10] = {0};
int i = 0;
int j = 0;
sprintf(strInput, "%d", val);
for (i = strlen(strInput) - 1; i >= 0; i--) {
if (strInt[strInput[i] - '0'] == 1) {
continue;
} else {
strInt[strInput[i] - '0'] = 1;
strOutput[j++] = strInput[i];
}
}
strOutput[j] = '\0';
return atoi(strOutput);
}
unsigned int HandleValue2(unsigned int val)
{
char strInt[10] = {0};
char strOutput[10] = {0};
int i = 0;
do {
int tmp = val % 10;
if (strInt[tmp] == 0) {
strInt[tmp] = 1;
strOutput[i++] = tmp + '0';
}
} while (val /= 10);
return atoi(strOutput);
}
int main()
{
FILE *fp1;
FILE *fp2;
char strInput[MAX_LINE] = " Hello, Life is what you can make it!, fighting! ";
char strInput2[MAX_LINE] = "Things can only get better";
char strOutput[MAX_LINE] = {0};
int val = Handleblank(strInput, strOutput);
printf("%d\n", val);
printf("%s\n", strInput);
printf("%s\n", strOutput);
char *strPtr[ARRAY_SIZE] = {NULL};
val = SeparateWords(strInput, strPtr);
printf("%d\n", val);
for (int i = 0; i < val; i++) {
printf("%s\n", strPtr[i]);
}
for (int k = 0; k < val; k++) {
free(strPtr[k]);
strPtr[k] = NULL;
}
printf("%s\n", strInput2);
val = SeparateWords(strInput2, strPtr);
printf("%d\n", val);
for (i = 0; i < val; i++) {
printf("%s\n", strPtr[i]);
}
for (k = 0; k < val; k++) {
free(strPtr[k]);
strPtr[k] = NULL;
}
// fclose(fp1);
// fclose(fp2);
return 0;
}