In C: I need help up implementing this function that takes a line and breaks it into words without using sscanf
/*
This function takes a line and breaks it into words.
The orginal line is in the char array str, the first word
will go into the char array c, the second into p1, and the
the third into p2. If there are no words, the corresponding
char arrays are empty. At the end, n contains the number of
words read.
*/
void breakUp(char *str, char *c, char *p1, char *p2, int *n)
{
c[0] = p1[0] = p2[0] = '\0';
/* YOU CANNOT USE THE FOLLOWING! */
// cannot use sscanf: *n = sscanf(str,"%s %s %s %*s", c, p1, p2);
// break up manually
}
Please find properly commented and working code below :
------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*
This function takes a line and breaks it into words.
The orginal line is in the char array str, the first word
will go into the char array c, the second into p1, and the
the third into p2. If there are no words, the corresponding
char arrays are empty. At the end, n contains the number of
words read.
*/
void breakUp(char *str, char *c, char *p1, char *p2, int *n)
{
c[0] = p1[0] = p2[0] = '\0';
/* YOU CANNOT USE THE FOLLOWING! */
// cannot use sscanf: *n = sscanf(str,"%s %s %s %*s",
c, p1, p2);
// break up manually
char * ptr = str;
//Temporary pointer for traversing string
int count = 0;
//Temporary counter
*n = 0;
if (*ptr == '\0')
//If string is empty, return
return;
while(*ptr != ' ' && *ptr !=
'\0'){ //Read the first word and put
in c[]
*(c + count) = *(ptr);
count++;
ptr++;
}
*n = 1;
//Update the n and reset count variables
c[count] = '\0';
if (*ptr == '\0')
return;
else
ptr++;
count = 0;
while(*ptr != ' ' && *ptr !=
'\0'){ //Read the first word and put
in c[]
*(p1 + count) = *(ptr);
count++;
ptr++;
}
*n = 2;
//Update the n and reset count variables
p1[count] = '\0';
if (*ptr == '\0')
return;
else
ptr++;
count = 0;
while(*ptr != ' ' && *ptr != '\0'){
*(p2 + count) = *(ptr);
count++;
ptr++;
}
*n = 3;
p2[count] = '\0';
return;
}
int main()
{
char str[100];
printf("Enter the string to be split : \n");
scanf("%[^\t\n]s", str);
char c[100], p1[100],p2[100];
int n;
breakUp(str, c, p1,p2,&n);
printf("n = %d\n", n);
printf("First word is : %s\n", c);
if(n>=2)
printf("Second word is : %s\n",
p1);
if(n>2)
printf("Third word is : %s\n",
p2);
}
------------------------------------------------------------------
Please find the screenshot for working code :

In C: I need help up implementing this function that takes a line and breaks it...
Suppose the following variable are defined in a function, char line[80], comm[20], p1[20], p2[20]; int n; Where line is the complete line entered by the user, comm is the first word on the line, p1 and p2 are the second and third words (if present), and n is the number of words on the line. If you were to pass these to a function called breakup, what would the function call look like? What would the function header look...
C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...
Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...
Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...
has to be in C++ for Xcode
10. Write a function void TrimString(char str[]); that takes a string such as ”This is a line ” and removes all spaces to produce a string ”Thisisaline”.
I need help implementing a few functions in c++. Any help would be appreciated 1. a function void PairsAddUpToK(int a[], int a_len, int k) that finds all pairs of numbers in an array that add up to a given number k 2. A function void TriplesAddUpToK(int a[], int a_len, int k) that finds all sets of three numbers in an array that ad up to a given number k 3. A function void SubsetsAddUpToK((int a[], int a_len, int k) that...
C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything. Your function should be named...
I need help with the creation of the pre and post conditions for each function of my program. Please see below. #include <iostream> #include <string.h> using namespace std; #define TOTAL_COMMANDS 7 struct command { char name[1024]; // Store the name of the command int *paramsCount; // Stores the valid no. of params char error[4096]; // Stores the valid errors int differentParams; // Store no. of different ways to specify params int differentErrors; // Store no. of different ways to error...
Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions. int letter_count(char *s) computes and returns the number of English letters in string s. int word_count(char *s) computes and returns the number of words in string s. void lower_case(char *s) changes upper case to lower case of string s. void trim(char *s) removes the unnecessary empty spaces of string s. mystring.h #include <stdio.h> int letter_count(char *); void lower_case(char *); int word_count(char *); void trim(char...
Due to lack of time, I need help writing this c++ function and
sample driver. I will add the description below. Any help would be
greatly appreciated. Thank you.
Write a function with two C++ strings as parameters void addS( const char origl], char plural]) The function will make a copy of orig to the C++ string The function will make a copy of orig to the C+t string plural with an 's at the end. A sample driver would...