SlideShare uma empresa Scribd logo
1 de 3
please write this code in C, not C++ or java
also assume the string library has been included
Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define
a SINGLETON to be a data element that is not repeated immediately before or after itself in the
sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first
appearance of "bird", "mammal", and the second appearance of "fish").
Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The
code assigns to the variable n the number of SINGLETONs that were read. (For example in the
above data sequence it would assign 4 to n ). Assume that n has already been declared but
notinitialized . Assume that there will be at least one word before the terminating "xxxxx".
Assume that the maximum length of a string is 8.
Solution
#include <stdio.h>
#include <string.h>
int isAvail(char words[20][20], int len,char word[])
{
int i;
for(i=0;i<len;i++)
{
if(strcmp(words[i],word)==0)
{
return 1;
}
}
return 0;
}
void SINGLETON(char str[],int *p)
{
int i,j=0;
*p=0;
char words[20][20];
int len=0;
char prev[20];
char curr[20];
char next[20];
strcpy(curr,"");
strcpy(prev,"");
strcpy(next,"");
for(i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
curr[j++]=str[i];
}
else if(str[i]==' ')
{
j=0;
if(strcmp(prev,curr)!=0 && strcmp(curr,next)!=0 && isAvail(words,len,curr)==0)
{
(*(p))++;
}
strcpy(words[len++],curr);
strcpy(prev,"");
strcpy(prev,curr);
strcpy(curr,"");
strcpy(curr,next);
strcpy(next,"");
}
}
}
int main()
{
int n;
char str[]="fish bird reptile reptile bird bird bird mammal fish";
SINGLETON(str,&n);
printf("number of singletons: %d",n);
return 0;
}
please write this code in C- not C++ or java also assume the string li.docx

Mais conteúdo relacionado

Semelhante a please write this code in C- not C++ or java also assume the string li.docx

Semelhante a please write this code in C- not C++ or java also assume the string li.docx (20)

Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Array
ArrayArray
Array
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Day2
Day2Day2
Day2
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Python l3
Python l3Python l3
Python l3
 
Arrays
ArraysArrays
Arrays
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Pythonppt28 11-18
Pythonppt28 11-18Pythonppt28 11-18
Pythonppt28 11-18
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE
 
Python ppt
Python pptPython ppt
Python ppt
 
Template Haskell
Template HaskellTemplate Haskell
Template Haskell
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdf
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 

Mais de rtodd884

Poly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docxPoly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docxrtodd884
 
Please write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docxPlease write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docxrtodd884
 
Please respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docxPlease respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docxrtodd884
 
Problem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docxProblem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docxrtodd884
 
Presented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docxPresented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docxrtodd884
 
Presented below is information related to Dino Radja Company- Date End.docx
Presented below is information related to Dino Radja Company- Date End.docxPresented below is information related to Dino Radja Company- Date End.docx
Presented below is information related to Dino Radja Company- Date End.docxrtodd884
 
Prepare the journal entry to record salaries payable for the month of.docx
Prepare the journal entry to record salaries payable for the month of.docxPrepare the journal entry to record salaries payable for the month of.docx
Prepare the journal entry to record salaries payable for the month of.docxrtodd884
 
Prepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docxPrepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docxrtodd884
 

Mais de rtodd884 (8)

Poly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docxPoly-meta-morphic malware looks different each time it is stored on di.docx
Poly-meta-morphic malware looks different each time it is stored on di.docx
 
Please write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docxPlease write a formal summary of the case before proceeding to answer.docx
Please write a formal summary of the case before proceeding to answer.docx
 
Please respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docxPlease respond in detail with an example- Thank you! Financial interme.docx
Please respond in detail with an example- Thank you! Financial interme.docx
 
Problem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docxProblem 1- Text Editor using files and strings using C++ In this proje.docx
Problem 1- Text Editor using files and strings using C++ In this proje.docx
 
Presented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docxPresented below is the balance sheet of Sargent Corporation for the cu.docx
Presented below is the balance sheet of Sargent Corporation for the cu.docx
 
Presented below is information related to Dino Radja Company- Date End.docx
Presented below is information related to Dino Radja Company- Date End.docxPresented below is information related to Dino Radja Company- Date End.docx
Presented below is information related to Dino Radja Company- Date End.docx
 
Prepare the journal entry to record salaries payable for the month of.docx
Prepare the journal entry to record salaries payable for the month of.docxPrepare the journal entry to record salaries payable for the month of.docx
Prepare the journal entry to record salaries payable for the month of.docx
 
Prepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docxPrepare a short --talking points-- paper in which you identify and dis.docx
Prepare a short --talking points-- paper in which you identify and dis.docx
 

Último

PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
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 ModeThiyagu K
 

Último (20)

PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
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
 
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
 

please write this code in C- not C++ or java also assume the string li.docx

  • 1. please write this code in C, not C++ or java also assume the string library has been included Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". Let's define a SINGLETON to be a data element that is not repeated immediately before or after itself in the sequence. So, here there are four SINGLETONs (the first appearance of "fish", the first appearance of "bird", "mammal", and the second appearance of "fish"). Write some code that uses a loop to read a sequence of words, terminated by the "xxxxx". The code assigns to the variable n the number of SINGLETONs that were read. (For example in the above data sequence it would assign 4 to n ). Assume that n has already been declared but notinitialized . Assume that there will be at least one word before the terminating "xxxxx". Assume that the maximum length of a string is 8. Solution #include <stdio.h> #include <string.h> int isAvail(char words[20][20], int len,char word[]) { int i; for(i=0;i<len;i++) { if(strcmp(words[i],word)==0) { return 1; } } return 0; } void SINGLETON(char str[],int *p) { int i,j=0; *p=0;
  • 2. char words[20][20]; int len=0; char prev[20]; char curr[20]; char next[20]; strcpy(curr,""); strcpy(prev,""); strcpy(next,""); for(i=0;i<strlen(str);i++) { if(str[i]!=' ') { curr[j++]=str[i]; } else if(str[i]==' ') { j=0; if(strcmp(prev,curr)!=0 && strcmp(curr,next)!=0 && isAvail(words,len,curr)==0) { (*(p))++; } strcpy(words[len++],curr); strcpy(prev,""); strcpy(prev,curr); strcpy(curr,""); strcpy(curr,next); strcpy(next,""); } } } int main() { int n; char str[]="fish bird reptile reptile bird bird bird mammal fish"; SINGLETON(str,&n); printf("number of singletons: %d",n); return 0; }