Question

In: Computer Science

I need convert this java code to C language. There is no string can be used...

I need convert this java code to C language. There is no string can be used in C. Thank you!

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( "\nEnd 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 );
}
  
}

Solutions

Expert Solution

Following is the C language implementation of the given Java code. The code uses character array to process strings.

C CODE:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

// Function to extract the word, from the given index.
const char * ExtractWord(char line[], int word, char result[]) {
   int i, j =0, ctr = 0;
   char wordList[10][10]; // Array to store each word separately.

   // For loop separates the line into separate words.
   for(i=0;i<=(strlen(line));i++) {
        // Assign NULL into wordList[], if NULL or spaces are encountered in the line.
        if(line[i]==' '||line[i]=='\0') {
            wordList[ctr][j]='\0';
            ctr++; // Scan next word
            j=0;    // index is set to 0 for a new word
        }
        else {
            wordList[ctr][j]=line[i];
            j++;
        }
   }
  
   // Check if index exceeds word limit.
   if(strlen( *wordList) < word) {
       printf("\nIndex is out of bound, exiting now.");
       exit(1);
   }
   // Extract the word from the given index from wordList[] and assign it to result.
    for(i=0;i < ctr;i++) {
       if(i == word) {
           strcpy(result, wordList[i]);
       }
   }
  
   return result;
}

// Main driver function
int main() {
    char line[100], result[20];
    int word, i, j;

    printf("\nEnter a line: ");
   scanf("%[^\n]",line);
   getchar(); // Get rid of newline char after scanf()
  
    printf("\nEnter the word index to extract: ");
    scanf(" %d", &word);

   // Invoke the function to extract the word
   strcpy(result, ExtractWord(line, word-1, result));
  
   printf("\nLine is: %s", line);
   printf("\nExtracted word at index %d is %s.", word, result);
  
   return 0;
}

OUTPUT:

Feel free to ask any doubt related to this answer in the comment section below. Do leave a thumbs up if this helps.


Related Solutions

JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory If(shared memory not full) { //sendthe string to the shared memory //send signal to process; }
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
In Java, how can I convert a string user input for example a student starts a...
In Java, how can I convert a string user input for example a student starts a uni degree in "Winter/2022", to a date type, and then once I have done that, add 3 years to that value, and changing the season, so for example Student Started: Winter/2022 and Student Finished: Summer/2025. Is there any possible way to do this? Thank you.
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT