SlideShare a Scribd company logo
1 of 8
I need some help to correct this code
Instructions
The following program takes a string and builds an index of it. The index indicates the:
Start of each line in the string,
Start of each word in the string,
Start of each number in the string.
stringhelp.h
#pragma once
#ifndef STRINGHELP_H #define STRINGHELP_H
#define MAX_STRING_SIZE 511 #define MAX_INDEX_SIZE 100 #define
MAX_WORD_SIZE 23
struct StringIndex
{
char str[MAX_STRING_SIZE + 1];
int wordStarts[MAX_INDEX_SIZE];
int lineStarts[MAX_INDEX_SIZE];
int numberStarts[MAX_INDEX_SIZE]; int numWords, numLines, numNumbers;
};
/**
* Return the index of the next whitespace.
* @param str - the string to search
* @returns the index of the next white space or the position of the string terminator.
*/
int nextWhite(const char* str);
/**
* Return true if the string contains only digits. * @param str - the string to check
* @param len - the number of characters to check * @returns true if the string is only digits.
*/
int isNumber(const char* str, const int len);
/**
* Build an index for a string showing the start of the lines,
* words, and numbers in the string.
* @param str - the string to search
* @returns the index of the next white space or -1 if there is none. */
struct StringIndex indexString(const char* str);
/**
* Return the number of lines in the index.
* @param idx - the index to get the number of lines in it * @returns the number of lines in the
index
*/
int getNumberLines(const struct StringIndex* idx);
/**
* Return the number of words in the index.
* @param idx - the index to get the number of words in it * @returns the number of words in the
index
*/
int getNumberWords(const struct StringIndex* idx);
/**
* Return the number of numbers in the index.
* @param idx - the index to get the number of numbers in it * @returns the number of numbers
in the index
*/
int getNumberNumbers(const struct StringIndex* idx);
/**
* Return the nth word from the index
* @param word - where the result will be placed
* @param idx - the index to use
* @param wordNum - the index of the word to retrieve
* @returns the word or an empty string if index is invalid
*/
void getWord(char word[], const struct StringIndex* idx, int wordNum);
/**
* Return the nth number from the index
* @param word - where the result will be placed
* @param idx - the index to use
* @param wordNum - the index of the number to retrieve
* @returns the number or an empty string if index is invalid */
void getNumber(char word[], const struct StringIndex* idx, int numberNum);
/**
* Return a pointer to the start of a line
* @param idx - the index to use
* @param lineNum - the index of the line to retrieve * @returns a pointer to the start of the line
*/
char* getLine(struct StringIndex* idx, int lineNum);
/**
* Prints characters until the terminator is found.
* @param s - the string to print
* @param start - the index to start printing
* @param terminator - the character to stop printing at when encountered.
*/
void printUntil(const char* s, const int start, const char terminator);
/**
* Prints characters until a space is found.
* @param s - the string to print
* @param start - the index to start printing
*/
void printUntilSpace(const char* s, const int start);
#endif
stringhelp.c
#define _CRT_SECURE_NO_WARNINGS #include "stringhelp.h" #include <ctype.h>
#include <string.h>
#include <stdio.h>
int nextWhite(const char* str)
{
int i, result = -1;
for (i = 0; result < 0 && str[i] != '0'; i++) {
if (isspace(str[i]))
{
result = i;
} }
return (result < 0) ? i : result; }
int isNumber(const char* str, const int len)
{
int i, result = 1;
for (i = 0; i < len && result; i++)
{
result = result && isdigit(str[i]);
}
return result; }
struct StringIndex indexString(const char* str)
{
struct StringIndex result = { {0}, {0}, {0}, 0, 0, 0 }; int i, sp;
strcpy(result.str, str); if (str[0] != '0')
{
result.lineStarts[0] = 0;
result.numLines = 1; }
for (i = 0; str[i] != '0'; i++)
{
while (str[i] != '0' && isspace(str[i]))
{
if (str[i] == 'n')
{
result.lineStarts[result.numLines] = i + 1;
}
i++; }
sp = nextWhite(str + i);
if (isNumber(str + i, sp - i + 1))
{
result.numberStarts[result.numNumbers++] = i;
}
else
{
result.wordStarts[result.numWords++] = i;
}
i += sp - 1; }
return result; }
int getNumberLines(const struct StringIndex* idx)
{
return idx->numLines;
}
int getNumberWords(const struct StringIndex* idx)
{
return idx->numWords;
}
int getNumberNumbers(const struct StringIndex* idx)
{
return idx->numNumbers;
}
void getWord(char word[], const struct StringIndex* idx, int wordNum)
{
int i, sp, start;
word[0] = '0';
if (wordNum < idx->numWords && wordNum >= 0)
{
start = idx->wordStarts[wordNum]; sp = nextWhite(idx->str + start); for (i = 0; i < sp; i++)
{
word[i] = idx->str[i]; }
word[sp] = '0';
((struct StringIndex*)idx)->numWords--; }
}
void getNumber(char word[], const struct StringIndex* idx, int numberNum)
{
int i, sp, start;
word[0] = '0';
if (numberNum < idx->numNumbers && numberNum >= 0) {
start = idx->numberStarts[numberNum]; sp = nextWhite(idx->str + start);
for (i = 0; i < sp; i++)
{
word[i] = idx->str[start + i]; }
word[sp] = '0'; }
}
char* getLine(struct StringIndex* idx, int lineNum)
{
char* result = NULL;
if (lineNum < idx->numLines && lineNum >= 0)
{
result = idx->str + idx->lineStarts[lineNum];
}
return result; }
void printUntil(const char* s, const int start, const char terminator)
{
int i;
for (i = start; s[i] != '0' && s[i] != terminator; i++) printf("%c", s[i]);
}
void printUntilSpace(const char* s, const int start)
{
int i;
for (i = start; s[i] != '0' && !isspace(s[i]); i++) printf("%c", s[i]);
}
main.c
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h>
#include "stringhelp.h" #include <string.h>
#include <ctype.h>
int main(void)
{
char testStr[] = { "This is an string with embedded newline
characters andn12345 numbers inside it as well 7890" };
struct StringIndex index = indexString(testStr); int i;
printf("LINESn");
for (i = 0; i < index.numLines; i++)
{
printUntil(index.str, index.lineStarts[i], 'n');
printf("n"); }
printf("nWORDSn");
for (i = 0; i < index.numWords; i++)
{
printUntilSpace(index.str, index.wordStarts[i]);
printf("n"); }
printf("nNUMBERSn");
for (i = 0; i < index.numNumbers; i++)
{
printUntilSpace(index.str, index.numberStarts[i]);
printf("n"); }
return 0; }

More Related Content

Similar to I need some help to correct this code Instructions The following progr.docx

Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
amitbagga0808
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
bhim1213
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
fashioncollection2
 

Similar to I need some help to correct this code Instructions The following progr.docx (20)

#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
PathOfMostResistance
PathOfMostResistancePathOfMostResistance
PathOfMostResistance
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
C programming
C programmingC programming
C programming
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
strings
stringsstrings
strings
 
Keypoints c strings
Keypoints   c stringsKeypoints   c strings
Keypoints c strings
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdfIN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
c programming
c programmingc programming
c programming
 

More from Phil4IDBrownh

I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
Phil4IDBrownh
 
Human Resource Management ed 16- Chapter 12- page 445 Houston is among.docx
Human Resource Management ed 16- Chapter 12- page 445 Houston is among.docxHuman Resource Management ed 16- Chapter 12- page 445 Houston is among.docx
Human Resource Management ed 16- Chapter 12- page 445 Houston is among.docx
Phil4IDBrownh
 
I was excited about my new assignment on a special department project-.docx
I was excited about my new assignment on a special department project-.docxI was excited about my new assignment on a special department project-.docx
I was excited about my new assignment on a special department project-.docx
Phil4IDBrownh
 

More from Phil4IDBrownh (20)

I have also tried only B-C-D-E I got it wrong- Which ones are the corr.docx
I have also tried only B-C-D-E I got it wrong- Which ones are the corr.docxI have also tried only B-C-D-E I got it wrong- Which ones are the corr.docx
I have also tried only B-C-D-E I got it wrong- Which ones are the corr.docx
 
I have also tried these combinations which apparently where wrong B-C-.docx
I have also tried these combinations which apparently where wrong B-C-.docxI have also tried these combinations which apparently where wrong B-C-.docx
I have also tried these combinations which apparently where wrong B-C-.docx
 
I am looking at satellite images- I come across one and say that there.docx
I am looking at satellite images- I come across one and say that there.docxI am looking at satellite images- I come across one and say that there.docx
I am looking at satellite images- I come across one and say that there.docx
 
I am trying to fill out a program where the method definitions will b.docx
I am trying  to fill out a program where the method definitions will b.docxI am trying  to fill out a program where the method definitions will b.docx
I am trying to fill out a program where the method definitions will b.docx
 
I am having trouble with the math- Emily- who is single- has been offe.docx
I am having trouble with the math- Emily- who is single- has been offe.docxI am having trouble with the math- Emily- who is single- has been offe.docx
I am having trouble with the math- Emily- who is single- has been offe.docx
 
Human Relations Planning Consider that the human resource manager of y.docx
Human Relations Planning Consider that the human resource manager of y.docxHuman Relations Planning Consider that the human resource manager of y.docx
Human Relations Planning Consider that the human resource manager of y.docx
 
HTML- CSS- Javascript- React- Redux--- Questions- 1- SASS and LESS 2-.docx
HTML- CSS- Javascript- React- Redux--- Questions- 1- SASS and LESS  2-.docxHTML- CSS- Javascript- React- Redux--- Questions- 1- SASS and LESS  2-.docx
HTML- CSS- Javascript- React- Redux--- Questions- 1- SASS and LESS 2-.docx
 
Huckvale Corporation manufactures custom cabinets for kitchens- It use (2).docx
Huckvale Corporation manufactures custom cabinets for kitchens- It use (2).docxHuckvale Corporation manufactures custom cabinets for kitchens- It use (2).docx
Huckvale Corporation manufactures custom cabinets for kitchens- It use (2).docx
 
Huckvale Corporation manufactures custom cabinets for kitchens- It use (1).docx
Huckvale Corporation manufactures custom cabinets for kitchens- It use (1).docxHuckvale Corporation manufactures custom cabinets for kitchens- It use (1).docx
Huckvale Corporation manufactures custom cabinets for kitchens- It use (1).docx
 
Huckvale Corporation manufactures custom cabinets for kitchens- It use.docx
Huckvale Corporation manufactures custom cabinets for kitchens- It use.docxHuckvale Corporation manufactures custom cabinets for kitchens- It use.docx
Huckvale Corporation manufactures custom cabinets for kitchens- It use.docx
 
Hypertrophy May be reversible Is organ growth due to increased cell ce.docx
Hypertrophy May be reversible Is organ growth due to increased cell ce.docxHypertrophy May be reversible Is organ growth due to increased cell ce.docx
Hypertrophy May be reversible Is organ growth due to increased cell ce.docx
 
Human Resource Management ed 16- Chapter 12- page 445 Houston is among.docx
Human Resource Management ed 16- Chapter 12- page 445 Houston is among.docxHuman Resource Management ed 16- Chapter 12- page 445 Houston is among.docx
Human Resource Management ed 16- Chapter 12- page 445 Houston is among.docx
 
HPRS 2201 Chapter 4- Endocrine System Diseases and Disorders Course Ob.docx
HPRS 2201 Chapter 4- Endocrine System Diseases and Disorders Course Ob.docxHPRS 2201 Chapter 4- Endocrine System Diseases and Disorders Course Ob.docx
HPRS 2201 Chapter 4- Endocrine System Diseases and Disorders Course Ob.docx
 
I'm not sure how to go about using the RE expression and finding the m.docx
I'm not sure how to go about using the RE expression and finding the m.docxI'm not sure how to go about using the RE expression and finding the m.docx
I'm not sure how to go about using the RE expression and finding the m.docx
 
I was excited about my new assignment on a special department project-.docx
I was excited about my new assignment on a special department project-.docxI was excited about my new assignment on a special department project-.docx
I was excited about my new assignment on a special department project-.docx
 
I selected a random sample of 10 individuals who completed the Class S.docx
I selected a random sample of 10 individuals who completed the Class S.docxI selected a random sample of 10 individuals who completed the Class S.docx
I selected a random sample of 10 individuals who completed the Class S.docx
 
I think 42- 43- and 44 are true- but not too sure- Need help with 41 a.docx
I think 42- 43- and 44 are true- but not too sure- Need help with 41 a.docxI think 42- 43- and 44 are true- but not too sure- Need help with 41 a.docx
I think 42- 43- and 44 are true- but not too sure- Need help with 41 a.docx
 
I tried doing this myself and got stuck sorting out retained earnings.docx
I tried doing this myself and got stuck sorting out retained earnings.docxI tried doing this myself and got stuck sorting out retained earnings.docx
I tried doing this myself and got stuck sorting out retained earnings.docx
 
I selected a random sample of 10 individuals who completed the Class S (1).docx
I selected a random sample of 10 individuals who completed the Class S (1).docxI selected a random sample of 10 individuals who completed the Class S (1).docx
I selected a random sample of 10 individuals who completed the Class S (1).docx
 
How many ways can two items be selected from a group of six items- Use.docx
How many ways can two items be selected from a group of six items- Use.docxHow many ways can two items be selected from a group of six items- Use.docx
How many ways can two items be selected from a group of six items- Use.docx
 

Recently uploaded

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

I need some help to correct this code Instructions The following progr.docx

  • 1. I need some help to correct this code Instructions The following program takes a string and builds an index of it. The index indicates the: Start of each line in the string, Start of each word in the string, Start of each number in the string. stringhelp.h #pragma once #ifndef STRINGHELP_H #define STRINGHELP_H #define MAX_STRING_SIZE 511 #define MAX_INDEX_SIZE 100 #define MAX_WORD_SIZE 23 struct StringIndex { char str[MAX_STRING_SIZE + 1]; int wordStarts[MAX_INDEX_SIZE]; int lineStarts[MAX_INDEX_SIZE]; int numberStarts[MAX_INDEX_SIZE]; int numWords, numLines, numNumbers; }; /** * Return the index of the next whitespace. * @param str - the string to search * @returns the index of the next white space or the position of the string terminator. */ int nextWhite(const char* str); /** * Return true if the string contains only digits. * @param str - the string to check * @param len - the number of characters to check * @returns true if the string is only digits. */ int isNumber(const char* str, const int len); /** * Build an index for a string showing the start of the lines, * words, and numbers in the string.
  • 2. * @param str - the string to search * @returns the index of the next white space or -1 if there is none. */ struct StringIndex indexString(const char* str); /** * Return the number of lines in the index. * @param idx - the index to get the number of lines in it * @returns the number of lines in the index */ int getNumberLines(const struct StringIndex* idx); /** * Return the number of words in the index. * @param idx - the index to get the number of words in it * @returns the number of words in the index */ int getNumberWords(const struct StringIndex* idx); /** * Return the number of numbers in the index. * @param idx - the index to get the number of numbers in it * @returns the number of numbers in the index */ int getNumberNumbers(const struct StringIndex* idx); /** * Return the nth word from the index * @param word - where the result will be placed * @param idx - the index to use * @param wordNum - the index of the word to retrieve * @returns the word or an empty string if index is invalid */ void getWord(char word[], const struct StringIndex* idx, int wordNum); /** * Return the nth number from the index * @param word - where the result will be placed * @param idx - the index to use * @param wordNum - the index of the number to retrieve * @returns the number or an empty string if index is invalid */ void getNumber(char word[], const struct StringIndex* idx, int numberNum); /** * Return a pointer to the start of a line * @param idx - the index to use
  • 3. * @param lineNum - the index of the line to retrieve * @returns a pointer to the start of the line */ char* getLine(struct StringIndex* idx, int lineNum); /** * Prints characters until the terminator is found. * @param s - the string to print * @param start - the index to start printing * @param terminator - the character to stop printing at when encountered. */ void printUntil(const char* s, const int start, const char terminator); /** * Prints characters until a space is found. * @param s - the string to print * @param start - the index to start printing */ void printUntilSpace(const char* s, const int start); #endif stringhelp.c #define _CRT_SECURE_NO_WARNINGS #include "stringhelp.h" #include <ctype.h> #include <string.h> #include <stdio.h> int nextWhite(const char* str) { int i, result = -1; for (i = 0; result < 0 && str[i] != '0'; i++) { if (isspace(str[i])) { result = i; } } return (result < 0) ? i : result; } int isNumber(const char* str, const int len)
  • 4. { int i, result = 1; for (i = 0; i < len && result; i++) { result = result && isdigit(str[i]); } return result; } struct StringIndex indexString(const char* str) { struct StringIndex result = { {0}, {0}, {0}, 0, 0, 0 }; int i, sp; strcpy(result.str, str); if (str[0] != '0') { result.lineStarts[0] = 0; result.numLines = 1; } for (i = 0; str[i] != '0'; i++) { while (str[i] != '0' && isspace(str[i])) { if (str[i] == 'n') { result.lineStarts[result.numLines] = i + 1; } i++; } sp = nextWhite(str + i); if (isNumber(str + i, sp - i + 1)) { result.numberStarts[result.numNumbers++] = i;
  • 5. } else { result.wordStarts[result.numWords++] = i; } i += sp - 1; } return result; } int getNumberLines(const struct StringIndex* idx) { return idx->numLines; } int getNumberWords(const struct StringIndex* idx) { return idx->numWords; } int getNumberNumbers(const struct StringIndex* idx) { return idx->numNumbers; } void getWord(char word[], const struct StringIndex* idx, int wordNum) { int i, sp, start; word[0] = '0'; if (wordNum < idx->numWords && wordNum >= 0) { start = idx->wordStarts[wordNum]; sp = nextWhite(idx->str + start); for (i = 0; i < sp; i++) {
  • 6. word[i] = idx->str[i]; } word[sp] = '0'; ((struct StringIndex*)idx)->numWords--; } } void getNumber(char word[], const struct StringIndex* idx, int numberNum) { int i, sp, start; word[0] = '0'; if (numberNum < idx->numNumbers && numberNum >= 0) { start = idx->numberStarts[numberNum]; sp = nextWhite(idx->str + start); for (i = 0; i < sp; i++) { word[i] = idx->str[start + i]; } word[sp] = '0'; } } char* getLine(struct StringIndex* idx, int lineNum) { char* result = NULL; if (lineNum < idx->numLines && lineNum >= 0) { result = idx->str + idx->lineStarts[lineNum]; } return result; } void printUntil(const char* s, const int start, const char terminator) { int i; for (i = start; s[i] != '0' && s[i] != terminator; i++) printf("%c", s[i]);
  • 7. } void printUntilSpace(const char* s, const int start) { int i; for (i = start; s[i] != '0' && !isspace(s[i]); i++) printf("%c", s[i]); } main.c #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include "stringhelp.h" #include <string.h> #include <ctype.h> int main(void) { char testStr[] = { "This is an string with embedded newline characters andn12345 numbers inside it as well 7890" }; struct StringIndex index = indexString(testStr); int i; printf("LINESn"); for (i = 0; i < index.numLines; i++) { printUntil(index.str, index.lineStarts[i], 'n'); printf("n"); } printf("nWORDSn"); for (i = 0; i < index.numWords; i++) { printUntilSpace(index.str, index.wordStarts[i]); printf("n"); } printf("nNUMBERSn"); for (i = 0; i < index.numNumbers; i++)