Monday 22 February 2016

NPTEL (WEEK-5.3)

Programming Assignment 5.3: String Class

Due on 2016-02-27, 23:55 IST

Implement a custom string class with public functions

1) combine which takes as argument two strings s1, s2 and appends s2 to s1.
2) replace which takes as argument two characters, and replaces all occurrences of the first character by the second character in the string, if the first character is found, otherwise replace returns NOT FOUND.

In the main program take two strings as input (on first line), then take two characters as input.  Concatenate the two strings using combine.   Replace the first character in the concatenated string (if found) with the second character.

Implement following public functions for the  String Class.

  1. String String::operator+ (String str) : Create an new String object and copy the base String and then append content of String str. 
  2. void String::replace (char oldChar, char newChar) : Replace the "oldChar" if found with "newChar"
  3. char * String::getCharArray(): Return a Character array with content of String.
  4. bool String::find (char c): Return true if the character is present in the string else false.

Input:

The first two line contains two strings, string1 and string2. The second line contains two characters char1 and char2.

The two strings will contain only letters in English alphabet (both upper and lower case). The two characters will also be English letters.

Output:

First concatenate the two strings. In the result, all occurrences of char1 should be replaced with char2 and that should be printed out.If char1 is not present in the resultant string after concatenation, print NOT FOUND

PROGRAM:

/**********************************************************
 * String Class
 * CONCEPTS: Operator overloading,dynamic memory allocation.
 **********************************************************/
//#include<iostream>
#include<cstdio>
#define MAXLEN 100

using namespace std;

class String {                                                                                                                              
   private:                                                                                                                                    
      char *s;                                                                                                                                  
      int size;                                                                                                                                 
   public:                                                                                                                                     
       String();                                                                                                                                 
       String(char *str);                                                       //Create a String class from a character array                                                                                                                   
       int len();                                                                    //Return length of string                                                                                                                  
      String operator+ (String str);                                     //Return a new string with appending 2 strings         (this+str)                                                                                 
      void operator= (char *str);                                     //Assign a new string from char[]str                                                                                                      
      void replace (char oldChar, char newChar);            //replace all occurrences of oldChar by     newChar                                                                                             
      bool find (char c);                                                    //Is `c` is found in this string                                                                                                          
      char * getCharArray();                                            // return a char array with content of string                                                                                             
};


String::String() {
 s = NULL;
 size = 0;
}

String::String(char *str) {
  int len = 0;

  while(str[len]!='\0') {
    len++;
  }
  size = len;
  s = new char[len];
  
  for(int i = 0; i <=size; i++){
    s[i]=str[i];
  }
}

int String::len() {
  return size; 
}
void String::operator= (char *str) {
  
  int len = 0;
  int i = 0;
  
  while(str[len]!='\0') len++;
  size = len;
  
  if( s != NULL) delete []s;

  s = new char[len];
  
  for(int i = 0; i <= size; i++){
    s[i]=str[i];
  }
}


int main() {
  String s1,s2;
  char inputStr[MAXLEN];
  
  scanf("%s",inputStr);
  s1 = inputStr;

  scanf("%s",inputStr);
  s2 = inputStr;
  
  String s3 = s1 + s2;
  char oldChar,newChar;
  scanf(" %c %c",&oldChar, &newChar);
  if( s3.find(oldChar) ) {
    s3.replace(oldChar, newChar);
      printf("%s",s3.getCharArray());
  } else {
      printf("NOT FOUND");
  }
  return 0;





CODING :

String String::operator+ (String str){
    String newStr;
    newStr.s = new char[size+str.size];
  int i,j;
  for(i=0;i<size;i++)
    {
  newStr.s[i] = s[i];
  newStr.s[i+1] = '\0';
    }
  for(j=0;j<str.size;i++,j++)
    {
      newStr.s[i] = str.s[j];
      newStr.s[i+1] = '\0';
    }

    //printf("\nAfter :%s",newStr.s);
    return newStr;
}


void String::replace (char oldChar, char newChar) {
  int f=0,i=0,l=0;

   while(s[l]!='\0') l++;

  for(i=0;i<l;i++)
  {
    if(s[i]==oldChar)
        s[i]=newChar;
  }

}

bool String::find (char c) {
  int f=0,i,l=0;

   while(s[l]!='\0') l++;

  for(i=0;i<l;i++)
  {
    if(s[i]==c)
    {
      f=1;
      break;
    }
  }
  if(f==0)
        return false;
    else
        return true;
}

char * String::getCharArray(){
    int i=0,l=0;
        while(s[l]!='\0') l++;
  char * str = new char[l+1];
  for(i=0;i<l;i++)
  {
     str[i] = s[i];
  }
  return str;
}




2 comments:

  1. hi kadvani,
    good job man ...But this coding doesn't pass few test case because in getchararray() function there should be null termination in the final string below function works fine

    char * String::getCharArray()
    {
    int i=0,l=0;
    while(s[l]!='\0')
    l++;
    char *str = new char[l+1];
    for(i=0;s[i]!='\0';i++) // this may also work fine
    {
    str[i] = s[i];
    }
    str[i]='\0';//here we need to terminate the string to avoid overflow
    return str;
    }

    ReplyDelete

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...