Please answer problem #5 thank you
str.c
#include "str.h"
#include <stdio.h>
int str_len(char *s)
{
/* this is here so code compiles */
return 0;
}
/* array version */
/* concantenate t to the end of s; s must be big enough */
void str_cat(char s[], char t[])
{
int i, j;
i = j = 0;
while (s[i] != '\0') /* find end of s
*/
i++;
while ((s[i++] = t[j++]) != '\0') /* copy t */
;
}
/* array version */
void squeeze(char s[], int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] =
s[i];
s[j] = '\0';
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = str_len(s) - 1; i < j; i++, j--
) {
/* change this so it calls cswap
*/
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* copy n chars of src into dest */
void pstr_ncpy(char *dest, char *src, int n)
{
}
/* concantenate t to the end of s; s must be big enough!
*/
void pstr_cat(char *s, char *t)
{
}
and the str.h file is:
#ifndef STR_H_
#define STR_H
void squeeze(char s[], int c);
void psqueeze(char *s, int c);
void str_cat(char s[], char t[]);
void pstr_cat(char *s, char *t);
void pstr_ncpy(char *dest, char *src, int n);
int str_len(char *s);
int pstr_ncmp(char *s, char *t, int n);
void cswap(char *c, char *d);
void preverse(char *s);
char *pindex(char *s, int c);
#endif
and here is test.c file:
#include <stdio.h>
#include "str.h"
#include <stdlib.h>
#include <string.h>
#define STRLEN
/* #define PSTRNCPY */
/* #define PSTRCAT */
/* #define PSTRNCMP */
/* #define PINDEX */
/* #define PSQUEEZE */
/* #define PREVERSE */
#define SIZE 100
int main(void)
{
char s[] = "c run, c run unix, run unix run";
#ifdef STRLEN
printf("the length of \"%s\" is %d\n", s,
str_len(s));
#endif
#ifdef PSTRNCPY
char s1[SIZE];
/* copy s into s1 */
pstr_ncpy(s1, s, str_len(s));
printf("s1 = %s\n", s1);
#endif
#ifdef PSTRCAT
char s1[SIZE];
char *s2 = ", go c, go!";
/* copy s into s1 */
pstr_ncpy(s1, s, str_len(s));
printf("s1 = %s\n", s1);
pstr_cat(s1, s2);
printf("s1 = %s\n", s1);
#endif
#ifdef PSTRNCMP
char *t = "look c, look c";
char *s2 = "look c, go c!";
int min;
int cmp;
/* use the smallest length */
min = str_len(s);
if (str_len(t) < min)
min = str_len(t);
printf("comparing:\n");
printf("%s\nand\n", s);
printf("%s\n", t);
if ((cmp = pstr_ncmp(s, t, min))) {
printf("strings differ in the first
%d chars; cmp = %d\n", min, cmp);
} else
printf("strings are the same for %d
chars; cmp = %d\n", min, cmp);
min = 8;
printf("\ncomparing:\n");
printf("%s\nand\n", t);
printf("%s\n", s2);
if ((cmp =pstr_ncmp(t, s2, min)))
printf("strings differ in the first
%d chars; cmp = %d\n", min, cmp);
else
printf("strings are the same for %d
chars; cmp = %d\n", min, cmp);
min++;
printf("\ncomparing:\n");
printf("%s\nand\n", t);
printf("%s\n", s2);
if ((cmp =pstr_ncmp(t, s2, min)))
printf("strings differ in the first
%d chars; cmp = %d\n", min, cmp);
else
printf("strings are the same for %d
chars; cmp = %d\n", min, cmp);
#endif
#ifdef PINDEX
printf("%s\n", s);
char c = 'x';
char *t = pindex(s, c);
printf("searching for %c in \"%s\"\n", c, s);
if (t != NULL)
printf("%c is located at address
%p\nwhich is %ld bytes from the base address %p\n", c, t, t - s,
s);
else
printf("char %c not found in
\"%s\"\n", c, s);
c = 'a';
t = pindex(s, c);
printf("searching for %c in \"%s\"\n", c, s);
if (t != NULL)
printf("%c is located at address
%p\nwhich is %ld bytes from the base address %p\n", c, t, t - s,
s);
else
printf("char %c not found in
\"%s\"\n", c, s);
#endif
#ifdef PSQUEEZE
int c = 'r';
printf("%s\n", s);
psqueeze(s, c);
printf("%s\n", s);
#endif
#ifdef PREVERSE
printf("%s\n", s);
printf("reversed is\n");
preverse(s);
printf("%s\n", s);
#endif
return 0;
}
// str.h
#ifndef STR_H_
#define STR_H_
void squeeze(char s[], int c);
void psqueeze(char *s, int c);
void str_cat(char s[], char t[]);
void pstr_cat(char *s, char *t);
void pstr_ncpy(char *dest, char *src, int n);
int str_len(char *s);
int pstr_ncmp(char *s, char *t, int n);
void cswap(char *c, char *d);
void preverse(char *s);
char *pindex(char *s, int c);
#endif
//end of str.h
// str.c
#include "str.h"
#include <stdio.h>
#include <stdlib.h>
int str_len(char *s)
{
/* this is here so code compiles */
int count=0;
for(int i=0;s[i]!='\0';i++)
count++;
return count;
}
/* array version */
/* concantenate t to the end of s; s must be big enough */
void str_cat(char s[], char t[])
{
int i, j;
i = j = 0;
while (s[i] != '\0') /* find end of s */
i++;
while ((s[i++] = t[j++]) != '\0') /* copy t */
;
}
/* array version */
void squeeze(char s[], int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] = s[i];
s[j] = '\0';
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = str_len(s) - 1; i < j; i++, j-- ) {
/* change this so it calls cswap */
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* copy n chars of src into dest */
void pstr_ncpy(char *dest, char *src, int n)
{
for(int i=0;i<n;i++)
*(dest+i) = *(src+i);
*(dest+n) = '\0';
}
/* concantenate t to the end of s; s must be big enough! */
void pstr_cat(char *s, char *t)
{
int i,j;
for(i=0;*(s+i)!='\0';i++);
for(j=0;*(t+j)!='\0';j++,i++)
*(s+i) = *(t+j);
*(s+i) = '\0';
}
int pstr_ncmp(char *s, char *t, int n)
{
for(int i=0;i<n;i++)
{
if(*(s+i) < *(t+i))
return -1;
else if(*(s+i) > *(t+i))
return 1;
}
return 0;
}
char *pindex(char *s, int c)
{
for(int i=0;*(s+i)!= '\0';i++)
if(*(s+i) == c)
return (s+i);
return NULL;
}
void psqueeze(char *s, int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (*(s+i) != c)
{
*(s+j) = *(s+i);
j++;
}
*(s+j) = '\0';
}
void cswap(char *c, char *d)
{
char temp = *c;
*c = *d;
*d = temp;
}
void preverse(char *s)
{
for(int i=0,j=str_len(s)-1;i<str_len(s)/2;i++,j--)
{
cswap((s+i),(s+j));
}
}
//end of str.c
// test.c
#include <stdio.h>
#include "str.h"
#include <stdlib.h>
#include <string.h>
/* #define STRLEN */
/* #define PSTRNCPY */
/* #define PSTRCAT */
/* #define PSTRNCMP */
/* #define PINDEX */
/* #define PSQUEEZE */
/* #define PREVERSE */
#define SIZE 100
int main(void)
{
char s[] = "c run, c run unix, run unix run";
#ifdef STRLEN
printf("the length of \"%s\" is %d\n", s, str_len(s));
#endif
#ifdef PSTRNCPY
char s1[SIZE];
/* copy s into s1 */
pstr_ncpy(s1, s, str_len(s));
printf("s1 = %s\n", s1);
#endif
#ifdef PSTRCAT
char s1[SIZE];
char *s2 = ", go c, go!";
/* copy s into s1 */
pstr_ncpy(s1, s, str_len(s));
printf("s1 = %s\n", s1);
pstr_cat(s1, s2);
printf("s1 = %s\n", s1);
#endif
#ifdef PSTRNCMP
char *t = "look c, look c";
char *s2 = "look c, go c!";
int min;
int cmp;
/* use the smallest length */
min = str_len(s);
if (str_len(t) < min)
min = str_len(t);
printf("comparing:\n");
printf("%s\nand\n", s);
printf("%s\n", t);
if ((cmp = pstr_ncmp(s, t, min))) {
printf("strings differ in the first %d chars; cmp = %d\n", min, cmp);
} else
printf("strings are the same for %d chars; cmp = %d\n", min, cmp);
min = 8;
printf("\ncomparing:\n");
printf("%s\nand\n", t);
printf("%s\n", s2);
if ((cmp =pstr_ncmp(t, s2, min)))
printf("strings differ in the first %d chars; cmp = %d\n", min, cmp);
else
printf("strings are the same for %d chars; cmp = %d\n", min, cmp);
min++;
printf("\ncomparing:\n");
printf("%s\nand\n", t);
printf("%s\n", s2);
if ((cmp =pstr_ncmp(t, s2, min)))
printf("strings differ in the first %d chars; cmp = %d\n", min, cmp);
else
printf("strings are the same for %d chars; cmp = %d\n", min, cmp);
#endif
#ifdef PINDEX
printf("%s\n", s);
char c = 'x';
char *t = pindex(s, c);
printf("searching for %c in \"%s\"\n", c, s);
if (t != NULL)
printf("%c is located at address %p\nwhich is %ld bytes from the base address %p\n", c, t, t - s, s);
else
printf("char %c not found in \"%s\"\n", c, s);
c = 'a';
t = pindex(s, c);
printf("searching for %c in \"%s\"\n", c, s);
if (t != NULL)
printf("%c is located at address %p\nwhich is %ld bytes from the base address %p\n", c, t, t - s, s);
else
printf("char %c not found in \"%s\"\n", c, s);
#endif
#ifdef PSQUEEZE
int c = 'r';
printf("%s\n", s);
psqueeze(s, c);
printf("%s\n", s);
#endif
#ifdef PREVERSE
printf("%s\n", s);
printf("reversed is\n");
preverse(s);
printf("%s\n", s);
#endif
return 0;
}
//end of test.c
Output:
Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...
Given the following source programs, write down the letter that correspond to the right answer #include <stdio.h> #ifndef MEMORY #include "myhead.h" #include <stdlib.h> myhead.h #include <string.h> int main() { #endif char *text= MEM(SIZE, char); #define SIZE 10 COPY_TEXT (text, MSG); #define MSG "CS262" newtext (text); #define MEM(size, type) SHOW (text); (type *) malloc(size * sizeof(type)) free (text); #define COPY_TEXT (dest, source) return 0; strcpy (dest, source) } text.c #define SHOW (x) printf("Output: %s\n", x) void newtext (char *text) { int...
#include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...
Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...
C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }
int main(void) { char s1[50]="jack", s2[50]="jill", s3[50]; printf("%s\n",strcat(strcat(strcpy(s3,s2)," and "),s2)); printf("%i",strlen(s1)+strlen(s2)); } Output?
2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....
The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...