Python转C语言
para = "Letting go of a negative habit happens by making one positive choice at a time. " \
"Creating a desired achievement comes about in the same way."
para = para.split()
arr = []
num_a = 0
word_num = 0
word_line = 0
count_line = 0
max_line = 72
for p in para:
if p == 'a':
p = "//"+p+"//"
num_a += 1
word_num += 1
word_line = len(p)
count_line += word_line
arr.append(p)
arr = ' '.join(arr)
print(arr)
输出是
Letting go of //a// negative habit happens by making one positive choice at //a// time. Creating //a// desired achievement comes about in the same way.
判断句子中的a即可
#include <stdio.h>
#include <string.h>
int main()
{
char t[]={"Letting go of a negative habit happens by making one positive choice at a time. "
"Creating a desired achievement comes about in the same way."};
int len=strlen(t);
for(int i=0;i<len;i++)
{
if(t[i]=='a'&&i-1>0&&i+1<len-1)
{
if(t[i-1]==' '&&t[i+1]==' ')
{
printf("//%c//",t[i]);
}
else
{
printf("%c",t[i]);
}
}
else
{
printf("%c",t[i]);
}
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char para[] = "Letting go of a negative habit happens by making one positive choice at a time. "
"Creating a desired achievement comes about in the same way.";
int num_a = 0;
int word_num = 0;
int word_line = 0;
int count_line = 0;
int max_line = 72;
char *p = strtok(para, " ");
while (p)
{
if (strcmp(p, "a") == 0)
{
printf("//%s// ", p);
num_a++;
word_line += 5;
}
else
{
printf("%s ", p);
word_line += strlen(p);
}
word_num++;
count_line += word_line;
p = strtok(NULL, " ");
}
return 0;
}
只要修改a字符内容,不用那么麻烦,只需要找到特征字串" a ",然后输出为" //a// "即可
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char para[] = "Letting go of a negative habit happens by making one positive choice at a time. "
"Creating a desired achievement comes about in the same way.";
char findstr[] = " a ";
char replacestr[] = " //a// ";
int len = strlen(para);
int i=0;
for(i=0;i<len-3;i++)
{
if(memcmp(para+i,p,3) == 0)
{
printf("%s",replacestr);
i+=2;
}
else
printf("%c",para[i]);
}
printf("%s",para+len-3);
return 0;
}