输入一行字符串,输出相应的行数

#include <stdio.h>
int main()
{
 int ch = 0;
 int line = 1;
 int flag = 1;
 while ((ch = getchar()) != EOF)
 {
  if (flag)
  {
   printf("%d", line);
   line++;
      flag = 0;
  }
    if (ch == ‘\n‘)
  {
   flag = 1; 
  }
  putchar(ch);
 }
 return 0;
}
时间: 2024-08-13 09:36:49

输入一行字符串,输出相应的行数的相关文章

将控制台输入的每一行字符串,输出至txt文件当中

/** *    需求:将控制台输入的每一行字符串,输出至txt文件当中. /** * 需求:将控制台输入的每一行字符串,输出至txt文件当中. * 思路: * 1.首先想到BufferReader高级流读取一行字符串readLine方法. * 2.但是前提依赖于字符转换流ISR和低级节点流,这里是从控制台输入,节点流自然是System.in.如果是从文件输入,则节点流换成FIS即可 * 3.这样就能建立一条输入流了.BufferedReader(new InputStreamReader(Sy

C++输入一行字符串的一点小结

C++输入一行字符串的一点小结 原文链接: http://www.wutianqi.com/?p=1181 大家在学习C++编程时,一般在输入方面都是使用的cin. 而cin是使用空白(空格,制表符和换行符)来定字符串的界的. 这就导致了对于带有空格的字符串,比如"I Love C++奋斗乐园论坛" 只能读入"I",后面的都无法读入. 这时怎么办?  一.对于字符数组: 方法一:getline() 读入整行数据,它使用回车键输入的换行符来确定输入结尾. 调用方法:

【c语言】依次将10个数输入,要求输出其中最大的数

// 依次将10个数输入,要求输出其中最大的数 #include <stdio.h> int main() { int a[10]; int i; int temp; printf("请输入10个数:"); for( i = 0; i < 10; i++ ) { scanf("%d",&a[i]); } for( i = 0; i < 10; i++ ) { if( a[i] > a[i+1] ) { temp = a[i];

判断输入的字符串是否是回文数

<?phpfunction yuanyincount($str){ $str_len=strlen($str); $a_count=0; $e_count=0; $i_count=0; $o_count=0; $u_count=0; $other_count=0; //五种原因字母的数组,没写输出 $a_arr=array(); $e_arr=array(); $i_arr=array(); $o_arr=array(); $u_arr=array(); $other_arr=array();

转:C++输入一行字符串的一点小结

原文链接: http://www.wutianqi.com/?p=1181 大家在学习C++编程时,一般在输入方面都是使用的cin.而cin是使用空白(空格,制表符和换行符)来定字符串的界的.这就导致了对于带有空格的字符串,比如”I Love C++奋斗乐园论坛”只能读入”I”,后面的都无法读入.这时怎么办?  一.对于字符数组:方法一:getline()读入整行数据,它使用回车键输入的换行符来确定输入结尾.调用方法: cin.getline(str, len);第一个参数str是用来存储输入行

C++输入一行字符串 getline/get/getline

getline() :对于字符数组读入整行数据,它使用回车键输入的换行符来确定输入结尾.getline()将丢弃换行符.调用方法: cin.getline(str, len);第一个参数str是用来存储输入行的数组名称,第二个参数len是要读取的字符数. 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char str[30]; 7 cin.getline(str, 30); 8 cout << s

从键盘上输入一行字符串,统计单词个数

#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>int main(){char str[100]={'\0'};int count,i,len;while(fgets(str,sizeof(str),stdin)!=NULL) //fgets的用法{count=0;len=strlen(str);for(i=0;i<len;i++){if(isalpha(str

输入任意字符串 输出这个字符串从小到大的排序

1 #include <stdio.h> 2 #include <string.h> 3 4 5 char *strings; 6 int count; 7 void bubble(char *strings,int count) 8 { 9 register int m, n; 10 register char s; 11 for (m = 1; m<count; m++) 12 for (n = count - 1; n >= m; --n) 13 { 14 if

c++ 输入一行字符串

char str1[50]; //cin>>str1; 方式1 不能统计(录入)空格后的字符 cin.get(str1,50); //方式2 能统计空格后输入的字符 按回车键输入结束 get()会将换行符保存在序列里 //cin.getline(str1,50); //方式3 能统计空格后输入的字符 按回车键输入结束 getline()不会将换行符保存在序列里 //cout<<strlen(str1)<<endl; //cout<<sizeof(str1)&