Char* and While Loop is not working with eachother - c++

Here is code which compare s2 with s1, if they are the same it returns 0 and some other options, but while loop is not end-able and I can not find its problem, the only thing I know if I convert char* s2 into const char* s2 it will work fine.
#include <iostream>
using namespace std;
int cmp(char*,char*);
int main()
{
char* s1;
cout << "Please Enter First Word: "; cin >> s1;
char* s2;
cout << "Please Enter Second Word: "; cin >> s2;
cout << "The Result is: " << cmp(s1,s2) << endl;
return 0;
}
int cmp(char* s1, char* s2)
{
int i=0;
while (*(s2+i)!=0)
{
if (*(s2+i)>*(s1+i)) return 1;
if (*(s2+i)<*(s1+i)) return -1;
i++;
}
return 0;
}

You have undefined behaviour. You aren't allocating any space to store the string (you're not initialising s1 or s2 to point at any memory).
I suggest using std::string instead; it manages its own memory issues.

Consider:
char* s1;
cout << "Please Enter First Word: "; cin >> s1;
How big is s1 ? The problem is that your char pointers doesn't point to any memory block. You need to allocate memory with new.

As above said by Oli and karlphillip, your code must allocate memory for strings.
And the compare function can be like....
int cmp(char* s1, char* s2)
{
int i=0;
while ( *(s1+i)!=0 & *(s2+i)!=0 )
{
if (*(s2+i)>*(s1+i)) return 1;
if (*(s2+i)<*(s1+i)) return -1;
i++;
}
if(*(s1+i)==0 & *(s2+i)==0 )
return 0;
else if (*(s1+i)!=0)
return -1;
else
return 1;
}
Because, your cmp function will return 0 whenever your str1 is greater in length that str2 and str1 is identical to str2 upto str2's length.

Related

reverse string program is not working correctly

I tried writing a string reversing program in C++. Though it seemed really simple, Idk why I ain't getting the correct output.
Here's my code:-
#include<iostream>
#include<string>
using namespace std;
string Reverse_string( string &s ){
for( size_t i{}; i<s.size() ; i++ ){
s.at(i)= s.at( s.size()-1-i );
}
return s;
}
int main(){
cout<<"Enter the string you want to reverse: ";
string s{};
getline(cin,s);
cout<<"\nThe reversed string is :"<< Reverse_string(s) << endl;
return 0;
}
Here is my output:-
Enter the string you want to reverse: string
The reversed string is :gniing
Please help me finding the bug!
In your code you are assigning the values s.at(i)= s.at( s.size()-1-i ); and thus it is changing the previous value of s.at(i), but you should swap the values of respective indexes, s.at(i) with s.at(s.size()-1-i). And also you only need to traverse the first half of the string to swap with respective last half of the string.
This works fine.
#include<iostream>
#include<string>
using namespace std;
string Reverse_string( string &s ){
for( size_t i{}; i<s.size()/2 ; i++ ){ // here you only need to go half
swap(s.at(i), s.at( s.size()-1-i )); // swap is builtin function
}
return s;
}
int main(){
cout<<"Enter the string you want to reverse: ";
string s{};
getline(cin,s);
cout<<"\nThe reversed string is :"<< Reverse_string(s) << endl;
return 0;
}
Instead of using functions. You can just use the headerfile #include <algorithm>. There is a built in reverse function.
#include <iostream>
#include <algorithm>
int main()
{
std::string s;
std::cout << "Enter the string you want to reverse: ";
std::cin >> s;
std::reverse(s.begin(), s.end());
std::cout << "The reversed string is: " << s;
return 0;
}

Determine if 2 strings are anagrams

Write a function that accepts a c-string and a character as parameters. Returns the number of times the character appears in the string.
I also have to do this in my program:
Convert strings A and B to all lower case. For each letter in string A, call function on A and B.
This is the code I have so far:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
char tolower(char c) {
return c;
}
int countChars(const char* string, char ch)
{
int count = 0;
int i;
int length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == ch)
{
count++;
}
}
return count;
}
int main() {
char stringA[50];
char stringB[50];
cout << "Please enter first string: ";
cin.getline(stringA, 50);
cout << "Please enter second string: ";
cin.getline(stringB, 50);
cout << "String 1: " << stringA << endl;
cout << "String 2: " << stringB << endl;
if (strcmp(stringA, stringB) == 0) {
cout << "These strings are anagrams" << endl;
}
else {
cout << "These strings are not anagrams" << endl;
}
}
If you want to do so in a simple and readable way, consider the following code:
#include <string>
#include <iostream>
#include <algorithm>
bool is_anagram(std::string s1, std::string s2)
{
std::sort(s1.begin(), s1.end());
std::sort(s2.begin(), s2.end());
return s1 == s2;
}
int main(void)
{
std::string s1, s2;
std::cout << "Input first string: ";
getline(std::cin, s1);
std::cout << "Input second string: ";
getline(std::cin, s2);
if (is_anagram(s1, s2))
std::cout << "Strings are anagrams." << std::endl;
else
std::cout << "Strings are NOT anagrams." << std::endl;
return 0;
}
The function is_anagram() returns true when the sorted strings (used algorithm library for it) are same.
Note: The strings are case-sensitive. Hello and hello are different.
Hope it helps you. :D
Just sort the both the strings and then compare it.
sort(stringA, stringA+strlen(stringA))
sort(stringB, stringB+strlen(stringB))
just before the if statement.
You were checking if the 2 strings are exactly equal. But anagrams can have different arrangement of letters but must have same letters.
Though you need to check may need to check if the 2 strings are same (if you don't consider a string to be an anagram of itself).
PS- a google search would have instantly provided you ans.
The easiest way to do this is to count the number of each letter. Note you don't need to count every letter just the ones are in the string. To compare them store the results in a map.
#include <iostream>
#include <string>
#include <map>
#include <array>
class AnagramCheck
{
// I am going to be lazy and use map to store the counts.
// But you can do this much smarter and get a better complexity
// by using an array. But that involves more checks
std::map<char, int> data;
public:
explicit AnagramCheck(std::string const& str)
{
for(auto x: str) {
++data[std::tolower(x)]; // If you want to be smart ignore space/punctuation
}
}
bool operator==(AnagramCheck const& rhs) const
{
return data == rhs.data;
}
};
int main()
{
std::string line1;
std::getline(std::cin, line1);
AnagramCheck line1Check(line1);
std::string line2;
std::getline(std::cin, line2);
AnagramCheck line2Check(line2);
if (line1Check == line2Check) {
std::cout << "Anagram\n";
}
}

Reversing a cstring array in a function using pointers c++

I have an assignment that wants me to get a cstyle string (just 1 word) from the user then use a function to reverse its letters. The function has to take in 2 parameters, the first that is the destination string, and the other that is the source string. It will take whatever is in the source string, reverse it, then store the reversed version in the second string.
But everytime I compile, I typed in hello it prints out: \370\365\277\357\376. I just learned how to use pointers and cstyle strings so I don't really know how to use them and I think thats what is messing up my code. I'm dont understand it that well though so I cant figure out where I'm going wrong at.
Please let me know if you know what I'm doing wrong. Thank you!
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
void IsPalindrome(char *cstring);
void Reverse(char *str2[], char *str1[]);
int main() {
// user variables
char str1[81];
char reverse;
char str2[81];
strcpy (str2, str1);
//prompt user to input string
cout << "\nPlease input a string (80 chars max): ";
cin >> str1;
cout << "\nYour string is: " << str1 << endl;
//call function
Reverse(str2[81], str1[81]);
//Output reversed string
cout << "Your string reversed is: " << str2 << endl;
cout << "This is a " << "." << endl;
return 0;
}
void Reverse(char *str2, char *str1)
{
char* front, *rear;
int len = strlen(str1);
char temp;
front = str1;
rear = &str1[len - 1];
while(front < rear)
{
temp = *front;
*front = *rear;
*rear = temp;
front++;
rear --;
}
}
void IsPalindrome(char cstring)
{
}
Pointers make life harder, don't use them if you don't need them. As your assignment is to reverse a string, so just use string. string is const char* inside but it's much easier to handle.
Beside your problem with pointers and arrays in your code, you are getting your string from input using std::cin. Remember that this way you can't get strings that have white space one them (you will just get the first word).
Also there is some algorithm that do this kind of tasks for you these days but for educational reasons it's not bad to do it yourself.
Here is what you can do, in the code I mentioned places that you could use ready to use algorithms to do the task for you.
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>
void Reverse(std::string &inputStr);
int main()
{
// user variables
std::string str1 = "";
std::cout << "\nPlease input a string (80 chars max): ";
std::getline(std::cin, str1);
std::cout << "\nYour string is: " << str1 << std::endl;
Reverse(str1);
//You also can use stl algorithm to reverse string for you and don't do it manually like below, but as it is an assignment it would not be good
//std::reverse(str1.begin(), str1.end());
//Output reversed string
std::cout << "Your string reversed is: " << str1 << std::endl;
std::cout << "This is a " << "." << std::endl;
system("pause");
return 0;
}
void Reverse(std::string &str)
{
int n = str.length();
for(int i = 0; i < n / 2; i++)
{
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
//You also can use stl algorithm to do the swap for you like below
//std::swap(str[i], str[n - i - 1]);
}
}
NOTE: This example will reverse your original string that user entered, if you want to have the both original and reversed, you need to pass a copy of input to the function. something like this :
std::string str2(str1);
Reverse(str2);
std::cout << "Reversed : " << str2;
EDIT: To match your assignment requirements (have a function with two parameters and single word string) you can easily change the above code like this :
void Reverse(std::string inputStr, std::string &outputStr);
int main()
{
// user variables
std::string str1 = "";
std::string str2 = "";
std::cout << "\nPlease input a string (80 chars max): ";
std::cin >>str1;
std::cout << "\nYour string is: " << str1 << std::endl;
Reverse(str1, str2);
//You also can use stl algorithm to reverse string for you and don't do it manually like below, but as it is an assignment it would not be good
//std::reverse(str1.begin(), str1.end());
//Output reversed string
std::cout << "Your string original is: " << str1 << std::endl;
std::cout << "Your string reversed is: " << str2 << std::endl;
system("pause");
return 0;
}
void Reverse(std::string inputStr, std::string &outputStr)
{
outputStr = inputStr;
int n = outputStr.length();
for(int i = 0; i < n / 2; i++)
{
char temp = outputStr[i];
outputStr[i] = outputStr[n - i - 1];
outputStr[n - i - 1] = temp;
//You also can use stl algorithm to do the swap for you like below
//std::swap(str[i], str[n - i - 1]);
}
}
There is no need of using pointers for reversing a character array to another character array.
void Reverse(char *str2, char *str1)
{
int len = strlen(str1);
for(int n = 0; n< len; n++)
{
str2[n] = str1[len-n-1];
}
}

segmentation fault (core dumped) in C++ strings

#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
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

reverse of the string using recursion

Hey guys i get stuck in the unusual situation. This is my code, it works perfectly for returning the reverse of the string but it gives output with including the space so I don't want that space to be included in my programme output so anyone has suggestions about this plz share it... by the way this is my code :
#include <iostream>
using namespace std;
string reverse(string str, int size) {
if (size == -1)
return "";
else
{
char a;
a = str[size];
return a + reverse(str, size - 1);
}
}
int main() {
int size;
cout << "the size of the string : ";
cin >> size;
string str;
cout << "enter the word : ";
cin >> str;
cout << reverse(str, size);
}
Since you use std::string, you don't need to specify the size of the string, but use the std::string::size() or std::string::length() member functions. Also, a = str[size]; is problematic when size equals to the size of the string, since you perform an out of bound access (remember that C++ uses zero-based indexing). You can simplify the code a lot, ending up with
#include <iostream>
#include <cstddef> // for std::size_t
using namespace std;
string reverse(string str, std::size_t pos) {
return (pos == 0 ? "" : str[pos - 1] + reverse(str, pos - 1));
}
int main() {
string str;
cout << "enter the word : ";
getline(cin, str); // allow for spaces in the string
cout << reverse(str, str.size()) << endl;
}
Here, instead of using cin >> str, I used getline(cin, str), since cin reads up to the first whitespace, whereas getline allows to read strings that containg spaces.
Change the implementation of the function reverse to the following.
string reverse(string str ,int size){
if (size==-1)
return "";
else
{
char a;
a=str[size];
if (' ' == a )
return reverse(str,size-1)
else
return a+reverse(str,size-1);
}
}
Alternatively, do some pre-processing on th input.