sicily 6274. Mispelling

Description
Misspelling is an art form that students seem to excel at. Write a program that removes the n th character from an input string.
 
Input

The first line of input contains a single integer N , (1≤N≤1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing M , a space, and a single word made up of uppercase letters only. M will be less than or equal to the length of the word. The length of the word is guaranteed to be less than or equal to 80.

Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the misspelled word. The misspelled word is the input word with the indicated character deleted.
 
算是五道作业里最难的了,因为还完全没学过字符串和字符的处理。
还好这道题流传很广,找了几个答案琢磨了一下,研究了字符串的处理方式,自己组织出了解法。
题目意思很简单,先输入要处理几个词,然后是每个词要抽掉的字母的位置和单词。
 
答案
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char word[100]; /* 用来存字符串的数组,100是随意设的,够用就行 */
 6     int n, mis, i, j;
 7     
 8     scanf("%d", &n);/* 输入要处理几个词 */
 9     
10     for( i=1; i<=n; i++ )/* 当单词还没全部处理完的时候 */ 
11     {
12         scanf("%d %s", &mis, &word);/* 输入要处理的字母的位置和整个单词 */ 
13         
14         printf("%d ", i);/* 输出这是第几个要处理的单词 */ 
15         for ( j=0; j<strlen(word); j++ )/* 当没有处理完单词的每个字母(没到全长)的时候 */ 
16         {                                /* 因为数组序号的特性,j初识值是0,
17                                         循环到j=单词长度即处理到单词后一个单元时跳出 */ 
18             if ((j+1)!= mis)    /* 如果这个字母不在要抽掉的位置上 */ 
19             {
20                 printf("%c", word[j]); /* 输出这个字母 */
21             }                    /* 如果这个字母在要抽掉的位置上就会被跳过不输出 */
22         }
23         printf("\n")        ;/* 输出一个空行 */
24     }
25     return 0;
26 }

今天看了一点C PRIMER PLUS里关于字符串的部分,貌似可以利用字符串最后带一个/0解决,不过试了几遍编译出来都是一堆代码然后报错,暂时作罢

后记

发现为什么之前不能利用/0解决了……原来是斜杠方向错了,应该是\0

修改过后的代码如下,可以不用strlen函数和string.h库,更加简单

View Code
 1 // Problem#: 6274
 2 // Submission#: 1609796
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include<stdio.h>
 7 int main()
 8 {
 9     char word[100];
10     int n, mis, i, j;
11     
12     scanf( "%d", &n );
13     
14     for( i=1; i<=n; i++ )
15     {
16         scanf( "%d %s", &mis, &word );
17         
18         printf( "%d ", i );
19         
20         for ( j=0; word[j] != '\0'; j++ )
21         {
22             if ( (j+1) != mis )
23             {
24                 printf( "%c", word[j] );
25             }
26         }
27         
28         printf("\n");
29     }
30     
31     return 0;
32 }

网上还有一种解法是从要misspell的位置开始,用后一个字符逐个向前覆盖,不过在这题中并没有要求改变字符串的值,直接用跳过输出的方法更加简便易懂

除了用if(j+1 != mis)+输出语句以外,应该也可以用输出语句+if(j+1 == mis)+continue来跳过。个人倾向于前一种,毕竟continue不符合结构化的原则。

原文地址:https://www.cnblogs.com/joyeecheung/p/2750475.html