using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
namespace WindowsApplication1
{
class Test
{
public static void Main()
{
string s1 = Console.ReadLine();
int space = 0;
for (int i = 0; i < s1.Length; i++)
{
if (s1[i] == ' ')
{
space++;
}
}
space ++;
show("单词数"+space );
show("字母数"+(s1.Length -space +1).ToString ());
pause();
}
public static void show(string s)
{
Console.WriteLine(s);
}
public static void pause()
{
Console.ReadLine();
}
}
}
这样写。for语句你懂吧。
#include"stdio.h"
main()
{char c;int i=0,j=0,k=0,l=0;
while((c=getchar())!='\n')
{if(c>=65&&c<=90||c>=97&&c<=122) i++;
else if(c>=48&&c<=57) j++;
else if(c==32) k++;
else l++;}
printf("i=%d,j=%d,k=%d,l=%d\n",i,j,k,l);
}