#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World!\n");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch,n=1;
while( ch !=EOF)
{
ch=getchar();
if(ch=='\n')
{
printf(" %d",n);
n++;
}
putchar(ch);
}
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
signed char checkSum = -1;
signed char chInput;
while (chInput != '\n') // input: Hello world!
{
chInput = getchar();
checkSum += chInput;
putchar(chInput);
}
printf("\n%d\n", checkSum); // 102
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
const int g_ciStrLength = 1000;
int main()
{
int iSumFlag = 0; // 记录每行的字符串长度
int iSumMax = 0; // 记录输入的最大行的行数
char vecStr[g_ciStrLength][g_ciStrLength]; //记录输入的字符串
int iLineNo = 0; //记录行号
int iLongestNo = 0; // 记录最长字符串的行号
while ((vecStr[iLineNo][iSumFlag] = getchar()) != EOF)
{
++iSumFlag; // 计算当前行的字符串个数
if (vecStr[iLineNo][iSumFlag] == '\n')
{
if (iSumMax < iSumFlag)
{
iSumMax = iSumFlag;
iLongestNo = iLineNo;
}
++iLineNo;
iSumFlag = 0;
}
}
printf("\n");
//输出最长的字符串
for (int i = 0; i < iSumMax; ++i)
{
printf("%c", vecStr[iLongestNo][i]);
}
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20 /*max # of columns to process*/
#define MAX_INPUT 1000 /*max # of input & output lines*/
int ReadColumnNumber(int columns[], int max);
void ReArrange(char *output, char const *input, int n_columns, int const columns[]);
int main()
{
int n_columns; /* # of columns to process */
int columns[MAX_COLS]; /* the columns to process */
char input[MAX_INPUT]; /* array for input line */
char output[MAX_INPUT]; /* array for output line */
//Read the list of column numbers
n_columns = ReadColumnNumber(columns, MAX_COLS);
//Read, process and print the remaining lines of input.
while (gets(input) != NULL) //原文用gets函数,但是gets函数已经被弃�?
{
printf("Original input: %s\n", input);
ReArrange(output, input, n_columns, columns);
printf("Rearranged line: %s\n", output);
}
return EXIT_SUCCESS;
}
/*
** Read the list of column numbers, ignoring any beyond the specified
** maximum.
*/
int ReadColumnNumber(int columns[],int max)
{
int num = 0;
int ch;
// Get the numbers, stopping at eof or when a number is < 0.
while (num < max && scanf("%d", &columns[num]) == 1 && columns[num] >= 0)
{
num++;
}
/*
** Make sure we have an even number of inputs, as they are
** supposed to be paired.
*/
if (num %2 != 0)
{
puts("Last column number is not paired.");
exit(EXIT_FAILURE);
}
/*
** Discard the rest of the line that contained the final
** number.
*/
while ((ch = getchar()) != EOF && ch != '\n')
;
return num;
}
/*
处理一个输入行,方法是把指定列的字符连接在一起。输出行用NUL结尾。
*/
void ReArrange(char *output, char const *input, int n_columns, int const columns[])
{
int col;/* columns数组的下标 */
int output_col;/* 输出列计数器 */
int len;/* 输入行的长度 */
len = strlen(input);
output_col = 0;//需要输出字符的个数,同时作为标志位方便追加字符
/*
** 处理每对列号
*/
for (col = 0; col < n_columns; col += 2)
{
int nchars = columns[col + 1] - columns[col] + 1;
// 如果输入行没这么长,跳过这个范围。
if (columns[col] >= len)
continue;
// 如果输出数组已满,任务就完成。
if (output_col == MAX_INPUT - 1)
{
break;
}
/*
如果输出数组空间不够,只复制可以容纳的部分。
*/
if (output_col + nchars > MAX_INPUT -1)
nchars = MAX_INPUT - output_col - 1;
/*
** 观察输入行中多少个字符在这个范围里面。如果它小于nchars,
** 对nchars的值进行调整。
*/
if (columns[col] + nchars - 1 > len)
nchars = len - columns[col];
/*
** Copy the relevant data.
*/
strncpy(output + output_col, input + columns[col], nchars);
// strcpy(output + output_col, input + columns[col]);//好像没什么影响
output_col += nchars;
}
output[output_col] = '\0';
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20 /*max # of columns to process*/
#define MAX_INPUT 1000 /*max # of input & output lines*/
int ReadColumnNumber(int columns[], int max);
void ReArrange(char *output, char const *input, int n_columns, int const columns[]);
int main()
{
int n_columns; /* # of columns to process */
int columns[MAX_COLS]; /* the columns to process */
char input[MAX_INPUT]; /* array for input line */
char output[MAX_INPUT]; /* array for output line */
//Read the list of column numbers
n_columns = ReadColumnNumber(columns, MAX_COLS);
//Read, process and print the remaining lines of input.
while (gets(input) != NULL) //原文用gets函数,但是gets函数已经被弃�?
{
printf("Original input: %s\n", input);
ReArrange(output, input, n_columns, columns);
printf("Rearranged line: %s\n", output);
}
return EXIT_SUCCESS;
}
/*
** Read the list of column numbers, ignoring any beyond the specified
** maximum.
*/
int ReadColumnNumber(int columns[],int max)
{
int num = 0;
int ch;
// Get the numbers, stopping at eof or when a number is < 0.
while (num < max && scanf("%d", &columns[num]) == 1 && columns[num] >= 0)
{
num++;
}
// 将原本这里判断奇偶的语句删掉
while ((ch = getchar()) != EOF && ch != '\n')
;
return num;
}
/*
** Process a line of input by concatenating the characters from
** the indicated columns. The output line is then NUL terminated.
*/
void ReArrange(char *output, char const *input, int n_columns, int const columns[])
{
int col;/* subscript for columns array */
int output_col;/* output column counter */
int len;/* length of input line */
len = strlen(input);
output_col = 0;//需要输出字符的个数,同时作为标志位方便追加字符
/*
** Process each pair of column numbers.
*/
for (col = 0; col < n_columns; col += 2)
{
int nchars = columns[col + 1] - columns[col] + 1;
if (col + 1 < n_columns)
nchars=columns[col+1]-columns[col]+1;
else
nchars=len;
if (columns[col] >= len || output_col == MAX_INPUT - 1)
{
break;
}
/*
** If there isn't room in the output array, only copy
** what will fit.
*/
if (output_col + nchars > MAX_INPUT -1)
nchars = MAX_INPUT - output_col - 1;
/*
** Copy the relevant data.
*/
strncpy(output + output_col, input + columns[col], nchars);
// strcpy(output + output_col, input + columns[col]);//好像没什么影响
output_col += nchars;
}
output[output_col] = '\0';
}