Home > Back-end >  [for] Java into c
[for] Java into c

Time:02-09

//This program reads in lines of text and extracts the NTH word in each line.

import java.util.Scanner;

Public class Nthword
{
Public static void main (String args [])
{
The String line;
Int the word;
Scanner stdin=new Scanner (System. In);

While (stdin. HasNextLine ())
{
The line=stdin. NextLine ();
The word=stdin. NextInt ();
System. The out. Println (" a "+ word);

Stdin. NextLine ();//get rid of the newline after the int
System. The out. Println (" Read line: \ "" + line +" \ ", extracting word [" + word + "] ");
System. The out. Println (" Word # "+ Word +" is: "+ extractWord (line, Word));
}

Stdin. Close ();

System. The out. Println (" \ nEnd of processing ");

}

//returns the first word of the string
Private static String extractWord (String theLine, int word)
{
Int start;
Int the end;
Int Spaces=1;
The String result="";

//search for NTH non - blank character
For (start=0; Start & lt; TheLine. Length () & amp; & Spaces & lt; The word; Start++)
{
If (Character. IsSpaceChar (theLine. CharAt (start)))
{
Spaces++;
}
}

//only need to continue the if we haven 't gone past the end of the string
If (start{
//the next blank character is liberally
For (end=start; End;

//we now have the word
Result=theLine. Substring (start, end);
}

Return (result);
}

}
HasNextline nextInt in Java () () how to achieve within the C language,

CodePudding user response:

For example,
Char * s;
int n;
While ((s=gets ())!=NULL) {//here is equivalent to hasNextLine
Sscanf (s, "% d", & amp; N);//here is equivalent to nextInt
Printf (" % d ", n);
}

CodePudding user response:

Suggest that the original poster to describe demand, according to the requirement to use C ~
  • Related