Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Linux using by C++ 1) Try below and explain why we have an error. char *y; y=

ID: 3737034 • Letter: I

Question

In Linux using by C++

1) Try below and explain why we have an error.

     char *y;

     y="hello"; // this is ok

     strcpy(y, "hello"); // this is an error

2) Define a character pointer array and store/display strings as below.

     char * x[10];

     x[0]="hi"; x[1]="bye"; x[2]="hello";

     printf("%s %s %s ", x[0],x[1],x[2]);

3) Read the same sentence with gets() and fgets() and explain the difference.

      char x[100];

      printf("enter a sentence ");

      gets(x);                                               

      int slen=strlen(x);

      for(i=0;i<slen;i++){

         printf("%x ", x[i]);

      }

      printf(" enter the same sentence ");

      fgets(x, 100, stdin);

      slen=strlen(x);

      for(i=0;i<slen;i++){

         printf("%x ", x[i]);

      }

4) Use strtok to extract words from a sentence and store them in an array. Display the number of words as below. Use as many functions as possible to split your program into small pieces.

      algorithm:

          read a line

          tokenize

          display tokens

Enter a sentence

aa bcd e e ff aa bcd bcd hijk lmn al bcd

You entered aa bcd e e ff aa bcd bcd hijk lmn al bcd

There were 12 words:

aa

bcd

e

e

ff

aa

bcd

bcd

hijk

lmn

al

bcd

The original sentence was: aa bcd e e ff aa bcd bcd hijk lmn al bcd

Explanation / Answer

If you have any doubts or clarrifications, give me a comment...

1) Initially pointer has no memory to store value. It can stores address of another variable.

char *y; -- created pointer with no memory

y = "hello" // y means address, here we are assigning address of "hello" string to y. "hello" independently create an memory, that memory address will assign to y.

strcpy(y, "hello"); //here we are copying string "hello". In strcpy function, we are copying character by character into y. But "y" doesn't have any memory... so it rejects to store because of no memory to store, that's we will get segmentation fault error.

2)

In this question we are declared character pointer array i.e., char *x[10];

It stores 10 strings... but we need to assign strings directly as like previous example.

3)

gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte (""). No check for buffer overrun is performed.

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ("") is stored after the last character in the buffer.

4)

In C++

#include <iostream>

#include <cstring>

using namespace std;

int main()

{

string str;

char st[1000];

cout<<"Enter a sentence"<<endl;

getline(cin, str);

cout<<"You entered: "<<str<<endl;

int i = 0;

char *tok_arr[100];

strcpy(st, str.c_str());

tok_arr[i] = strtok(st, " ");

while(tok_arr[i]!=NULL){

i++;

tok_arr[i] = strtok(NULL, " ");

}

cout<<"There are "<<i<<" words: "<<endl;

int j=0;

while(j<i){

cout<<tok_arr[j]<<endl;

j++;

}

cout<<"The original sentence was: "<<str<<endl;

}

In C

#include<stdio.h>

#include<string.h>

int main()

{

char str[1000], dup[1000];

printf("Enter a sentence ");

scanf("%[^ ]s", str);

strcpy(dup, str);

printf("You entered: %s ", str);

int i = 0;

char *tok_arr[100];

tok_arr[i] = strtok(str, " ");

while(tok_arr[i]!=NULL){

i++;

tok_arr[i] = strtok(NULL, " ");

}

printf("There are %d words: ", i);

int j=0;

while(j<i){

printf("%s ",tok_arr[j]);

j++;

}

printf("The original sentence was: %s ",dup);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote