10-0. Talking ironically(20)
Given an English sentence, you are asked to write a program that reverses the order of all the words in the sentence and outputs it.
Input format: The test input contains a test case, giving a string with a total length of no more than 80 characters in one line. The string consists of a number of words and a number of spaces, where a word is a string of English letters (uppercase and lowercase are distinguished), and words are separated by a space. Make sure there are no extra spaces at the end of the sentence when inputting.
Output format: The output of each test case occupies one line, and the sentences in reverse order are output.
![](https://img-blog.csdnimg.cn/img_convert/cca217fb15a793fb482adac3ddfbf5a2.png)
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[81];//总长度不超过80的字符串 还加了最后一位\0
gets(str);//可以输入空格 以回车结束
char *p=str;
int sig=1;
//将指针挪到最后一个字符
while(*(p+1)!='\0'){
//comparison between pointer and integer
//指针要用单引号【‘’】
p ++;
}
//开始输出
while(sig)
{
if( *p==' '&& *(p+1)!=' ')
{
*p='\0';
printf("%s ",(p+1));
//printf("%s ",*(p+1)); 不会报错 但是无法输出
}
else if(p==str)//读到了第一位
{
sig=0;
printf("%s",p);
//printf("%s",*p);无法输出
}
p --;
}
}
printf("%s",*p);cannot output
printf("%s",p); cannot output the string pointed to by output p [ends with \0]
printf("%c",*p); output a character above the address pointed to by p
10-1. Search for specified characters in a string (15)
Enter a string S, then enter a character c, and it is required to find the character c in the string S. If not found, output "Not found"; if found, output all characters starting from c in string S.
Input format:
Input gives a non-empty string of no more than 80 characters in length, terminated by a carriage return, on line 1; a character on line 2.
Output format:
Output the results in one line as required by the question.
Input example 1:
![](https://img-blog.csdnimg.cn/img_convert/a2cfc16edffd574adf59a9aa35d7ba7f.png)
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[81];
gets(str);
//第1行中给出一个不超过80个字符长度的
//以回车结束的非空字符串;
char f;
scanf("%c",&f);
char *p=strchr(str,f);
if(p)//指针有指向地址就执行
{
printf("%s",p);
}
else{//指针为空
printf("not found");
}
}
10-2. Delete substrings in a string (20)
Input two strings S1 and S2, and it is required to delete all substrings S2 that appear in the string S1, that is, the resulting string cannot contain S2.
Input format:
Enter two non-empty strings of no more than 80 characters in length and ending with a carriage return in two lines, corresponding to S1 and S2.
Output format:
Output in one line the resulting string after deleting all substring S2 that appears in string S1.
![](https://img-blog.csdnimg.cn/img_convert/064071d6d706cfdb474f3cd4b5200e8f.png)
#include <stdio.h>
#include <string.h>
int main(void)
{
int i,k,sig=1;
int l;
char s1[81];
gets(s1);
char s2[81];//要删除的 cat
gets(s2);
char *s4=s1;
//输入在2行中分别给出不超过80个字符长度的、
//以回车结束的2个非空字符串,对应S1和S2
char s3[81];
k=strlen(s2);
l=k;
char *p=s2;//p指向cat
char *q=strchr(s1,*p);
char *ans=s2;//因为后面p-- 这里临时保存一下
while(sig)
{
char*q=strchr(s4,*p);//只查找cat的c
while(k)//cat三个都一致
{
if(*p==*q)
{
p ++;
q ++;
k --;
}
else{
sig=0;
continue;
}
}
if(sig)
{
// printf("p:%s\n",p);
// printf("ans:%s\n",ans);
// printf("q:%s\n",q);
strcpy(s3,q);
//printf("s1:%s\n",s1);
*(q-l)='\0';
// printf("s1:%s\n",s1);
strcat(s1,s3);
}
k=l;
p=s2;
}
printf("%s",s1);
}
correct answer:
#include <stdio.h>
#include <string.h>
char s1[81], s2[81];
int main()
{
int i, k;
gets(s1);//tomcatysscattom
gets(s2);
char *p = strstr(s1, s2);//指针pz指向第一个c tomcatcattom
while(1)
{
char *p = strstr(s1, s2);//catysscattom
if ( p )//如果还能找到cat
{
for ( i=0; i<strlen(p) - strlen(s2); i++ )
{
p[i] = p[strlen(s2) + i];//将后面的往前移
}
p[i] = '\0';//p:ysscattom\0
}
else
{
puts(s1);
break;
}
printf("此时s1=%s",s1); //tomysscattom
}
return 0;
}
Analysis:
The pointer points to a certain position in the string, and the content of the string can be modified using an array .
char s1[81];
int main()
{
gets(s1);//123456789
char *p = s1;
p[5]='\0';
printf("%s",s1);//12345
return 0;
}