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:   ...
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);
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 ,...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
I need this Java code translated into C Code. Thank you. //Logical is the main public...
I need this Java code translated into C Code. Thank you. //Logical is the main public class public class Logical { public static void main(String args[]) { char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}}; // table contains total combinations of p,q,& r int totalTrue, totalFalse, proposition;    //proposition 1: proposition=1; start(proposition); totalTrue=0; totalFalse=0; for(int i=0;i<8;i++) {    { char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0])); System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o); if(o=='T') totalTrue++; else totalFalse++;    } } finalOutput(totalTrue,totalFalse,proposition); System.out.println(" "); System.out.println(" ");    //proposition 2: proposition=2; start(proposition);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT