Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Take the Java program Nthword.java and convert it to the equivalent C program. i

ID: 3786551 • Letter: T

Question

 Take the Java program Nthword.java and convert it to the equivalent C program. import java.util.Scanner;  public class Nthword  {   public static void main( String args[] )   {     String line;     int word;     Scanner stdin = new Scanner(System.in);      while ( stdin.hasNextLine() )     {       line = stdin.nextLine();       word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int        System.out.println( "Read line: "" + line + "", extracting word [" + word + "]" );       System.out.println( "Word #" + word + " is: " + extractWord( line, word ) );     }      stdin.close();      System.out.println( " End of processing" );        }    // returns the first word of the string   private static String extractWord( String theLine, int word )   {     int start;     int end;     int spaces = 1;     String result = "";      // search for the nth non-blank character     for (start = 0; start < theLine.length() && spaces < word; start++)     {         if ( Character.isSpaceChar( theLine.charAt( start ) ) )         {             spaces++;         }     }      // only need to continue if we haven't gone past the end of the string     if ( start<theLine.length() )     {       // the next blank character is the end       for ( end=start ; end<theLine.length() && !Character.isSpaceChar( theLine.charAt( end ) ) ; end++ )         ;        // we now have the word        result = theLine.substring( start, end );     }      return( result );   }    } 

Explanation / Answer

#include<stdio.h>
#include<string.h>
int main()
{
String line;
int word;
   for (;;) {
printf ("Enter your string: ");
if (fgets (line, sizeof (line), stdin) == NULL)
break;
if (strcmp (line, " ") == 0)
break;
scanf("%d",&word);
printf( "Read line: "%s", extracting word [%d]",line,word );
printf( "Word # %d is: %s",wordextractWord( line, word ) );
}
stdin.close();
System.out.println( " End of processing" );
}

// returns the first word of the string
string extractWord( string theLine, int word )
{
int start;
int end;
int spaces = 1;
   int i;
string result = "";
// search for the nth non-blank character
for (start = 0; start < strlen(theLine) && spaces < word; start++)
{
if ( theLine[start] == ' ' )
{
spaces++;
}
}
// only need to continue if we haven't gone past the end of the string
if ( start<strlen(theLine) )
{
// the next blank character is the end
for ( end=start ; end<strlen(theLine) && theLine[ end ] != ' ' ; end++ )
;

// we now have the word
   int j=0;
   for (i=start; i<end;i++)
result[j++] = theLine[i];
}

return( result );
}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote