-2
#include<iostream>
#include<string>

using namespace std;
int STRLEN(char* s){
cout<<"\n1.";
int i=0;
while(s[i] != '\0'){
cout<<"\n2.";
        i++;
    }
    return i;
}
int main(){

int i,j;
char* s1;
char* s2;
cout<<"\nEnter string : ";
cin.getline(s1,50);
cout<<s1;
cout<<"\nEnter string : ";
cin.getline(s2,50);
cout<<s2;

int L1=STRLEN(s1);
int L2=STRLEN(s2);

cout<<"\nL1 = "<<L1;
cout<<"\nL2 = "<<L2;
/*
  for*(i=L1,j=0; i<L1+L2; i++,j++)
{
s1[i] = s2[j];
j++;
}
cout<<s1;*/
return 0;

}

the above code is giving me segmentation fault at line int L1=STRLEN(s1); Please provide a solution , i want my string to be dynamically manipulated, so that i can extend the given string, also append new string to existing string without using inbuilt methods. Also without using string data type

SHG
  • 2,516
  • 1
  • 14
  • 20
Nitish Prajapati
  • 139
  • 1
  • 11

1 Answers1

2

Actually, your function STRLEN looks norm (except couts inside and lack of const for s)

int STRLEN(const char* s)
{
   int i=0;
   while(s[i] != '\0')
   {
       i++;
   }
   return i;
}

The problem in memory allocation :getline does not allocate memory for you - you must allocate memory for strings

char* s1;
char* s2;

E.g. like:

char* s1 = malloc(100);
char* s2 = malloc(100);

Actually for your case with cin.getline(s2,50); 50 bytes will be enough:

char* s2 = (char*)malloc(50);

Here (char*) is explicit cast of a pointer type (see also static_cast for C++, and be informed that for C implicit cast is working in that case)

UPDATE:

Just to give you more examples and provoke more questions... the following is my modification of your program with comments for each section:

#include<iostream>
#include<string>
using namespace std;

int STRLEN(const char* s)
{
    int i=0;
    while(s[i] != '\0')
    {
        i++;
    }
    return i;
}

int main(void)
{
    int i; // one counter will be enough
    char* s1;
    char* s2;
    // allocation the memory
    s1 = static_cast<char*>(malloc(50));
    s2 = static_cast<char*>(malloc(50));
    // check results of memory allocation
    if(!s1 || !s2)
    {
      cerr << "Something went wrong!" << endl;
      return 1;
    }
    // getting strings
    cout<<"\nEnter the first string : ";
    cin.getline(s1,50);
    cout<< "S1 : [" << s1 << "]" << endl;
    // clean input buffer before next string input
    cin.clear(); // reset state of cin
    cin.ignore(INT_MAX, '\n'); //  clean the input buffer
    // continue input
    cout<<"\nEnter the second string : ";
    cin.getline(s2,50);
    cout<< "S2 : [" << s2 << "]" << endl;
    // count sizes (like strlen)
    int L1=STRLEN(s1);
    int L2=STRLEN(s2);
    // reallocate memory for resulting string in s1
    if( !(s1 = static_cast<char*>(realloc(s1, L1+L2+1))) )
    {
       cerr << "Something went wrong while reallocating memory!" << endl;
       return 1;
    }

    // manipulations with strings (like strcat)
    for(i=0; i <= L2; i++) // <= to copy '\0' also
    {
       s1[i + L1] = s2[i];
    }
    cout<< "Result : [" << s1 << "]" << endl;
    // deallocate memory
    free(s1);
    free(s2);
    return 0;
}

And as molbdnilo rightly noted in the comments, in C++ it is better to use new and delete for memory allocation and deallocation, so after you figure out with my example try to get rid of C functions: malloc, realloc and free.

After that, like making your program even more C++ solution, consider changing the type of strings from char * to std::string this will definitely save you from memory allocation problem and make all other parts of program simpler (e.g. s1 += s2 operation will be possible). When you get to that read about getline for string

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • No, i want them to be dynamic – Nitish Prajapati Jul 30 '17 at 08:27
  • that commented code is there for i want to append s2 to s1. – Nitish Prajapati Jul 30 '17 at 08:28
  • "be dynamic"? O.K. allocate and then reallocate with `realloc` – VolAnd Jul 30 '17 at 08:28
  • malloc gives foll error try.cpp: In function ‘int main()’: try.cpp:17:18: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive] char* s1 = malloc(100); ~~~~~~^~~~~ try.cpp:18:18: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive] char* s2 = malloc(100); – Nitish Prajapati Jul 30 '17 at 08:30
  • @NitishPrajapati Try `s1 = (char*)malloc(50);` – VolAnd Jul 30 '17 at 08:32
  • awesome! its working. How? what's that code. Please explain what it does. I'm a new learner you see. so these things i'm learning and want to learn stuff – Nitish Prajapati Jul 30 '17 at 08:40
  • could you suggest any tutorial series for such type of data handling – Nitish Prajapati Jul 30 '17 at 08:40
  • @NitishPrajapati You can start from http://www.cplusplus.com/doc/tutorial/typecasting/ Then you will be able to wriеу search requests for further questions – VolAnd Jul 30 '17 at 08:43
  • 2
    You should use `new` in C++. – molbdnilo Jul 30 '17 at 08:45
  • @NitishPrajapati I suggest instead of reading blogs and tutorials you should better go for a much systematic learning. You can check this link, for some good suggestions [link](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – WhiteSword Jul 30 '17 at 08:46
  • I don't know much about c++, but in c it is considered as a bad practice to cast a `malloc` return, read (this)[https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc] – WhiteSword Jul 30 '17 at 08:49
  • 1
    @ManjinderSinghHanjra In C that's correct. The cast is needed in C++ because there's no implicit cast from `void*` to another pointer type. – Blastfurnace Jul 30 '17 at 09:00
  • @Destructor Good suggestion! After studying `new` redesign to `string` can be the next step, that will not require `new`, but will require using `std::getline (std::cin,name);` – VolAnd Jul 30 '17 at 09:46