本文目录一览:
c语言you
是其中的一个字符还是整个数组
数组的话用for
字符的话需要借助第三个临时变量
c = a;
a= b;
b= c;
c语言 描述 编写一个程序实现将字符串中的所有”you”替换成”we”
编程水平有限 还没看到指针 只用了前面的内容 调试通过了
#includestdio.h
#includestring.h
int main()
{
char str[1000];
int i=0,strlong,flag[333],n; //strlong用来储存str的长度,flag用来储存you中u的位置
printf(“Please input string!\n”);
while((scanf(“%c”,str[i++])!=EOF)i=1000) //输入后按Enter,再按ctrl+z
{
continue;
}
strlong=strlen(str);
for(i=0,n=0;i=strlong;i++)
{
if(str[i]==’y’||str[i]==’Y’)
{
if(str[i+1]==’o’||str[i+1]==’O’)
{
if(str[i+2]==’u’||str[i+2]==’U’) //判断是否为单词you
{
if(str[i]==’Y’)
str[i]=’W’;
else //保证字母大小写相同
str[i]=’w’;
if(str[i+1]==’O’)
str[i+1]=’E’;
else
str[i+1]=’e’;
flag[n++]=i+2; //储存u的位置,便于将u覆盖
}
}
}
}
for(n=0;flag[n]!=’\0′;n++) //这里就是将u后面的字符向前移动一位
{ //替换一个you后因为后面的字符整体前移了
if(n==0)
i=flag[n];
else //所以flag中储存的u的位置就要减去1,替换两个后
i=flag[n]-n; //整体前移两位,就要减去2,以此类推
for(;i=strlong;i++)
{ //不清楚的话你可以吧for后的if…..else…去掉
str[i]=str[i+1]; //直接把i=flag[n]放在第二个for(i=flag[n];;)中
}
}
printf(“%s”,str);
return 0;
}
题目描述 (请用C语言) 编写一个C程序实现将字符串中的所有单词”you”替换成”we” 输入
#include “stdio.h”
#include “string.h”
bool IsSeparator(char ch)
{
return (!((ch = ‘a’ ch = ‘z’) || (ch = ‘A’ ch = ‘Z’)));
}
char* GetFirstWord(char *sentence)
{
char word[100];
int i;
for(i = 0; i 100; i++)
word[i] = ‘\0’;
for(i = 0; sentence[i] !IsSeparator(sentence[i]); i++)
word[i] = sentence[i];
return word;
}
void main()
{
char sentence[] = “you are what you do”, word[101];
char *p;
int i, j;
printf(sentence);
printf(“\n”);
i = 0;
p = sentence;
while(*p)
{
strcpy(word, GetFirstWord(p));
if(strcmp(word, “you”) != 0)
{
for(j = 0; j strlen(word); j++)
sentence[i++] = *(p++);
}
else
{
p = p + 3;
sentence[i++] = ‘w’;
sentence[i++] = ‘e’;
}
sentence[i++] = *p;
if(*p) p++;
}
printf(sentence);
printf(“\n”);
}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/196790.html