Possible memory leak in using dynamic memory? - c++

In this program, I coded to get a list of names, specifically using pointers, and perform bubble sort and binary search on the list. I recognize that there are better ways of dealing with this type of memory, but I want to learn this way properly before moving on. I was having errors with not properly deallocating and/or allocating memory, but now, I don't have any error messages popping up running this code, though I still believe that some memory has not been properly deallocated. Here's my code:
Main.cpp
#include "Functions.h"
void main() {
// initialize variables
int numNames = 0;
char* search;
int index = 0;
bool isSearching = true;
// get list of names
cout << "Enter your list of names:" << endl;
char** nameList = readList(numNames);
// sort list of names
bubbleSort(nameList, numNames);
// show sorted list of names
cout << "Sorted list of names:" << endl;
for (int i = 0; i < numNames; i++) {
cout << nameList[i] << endl;
}
// do binary search loop
do {
// get search word
cout << "Enter a word you would like to search for: " << endl;
search = readLine();
// get index of search
index = binarySearch(nameList, numNames, search);
// if missing
if (index == -1) {
cout << "That word is missing" << endl;
}
// show index
else {
cout << "That word is at index " << index << endl;
}
// set loop test
isSearching = search[0] != '\0';
// free mem
delete[] search;
} while (isSearching);
// free mem
for (int i = 0; i < numNames; i++) {
delete[] nameList[i];
}
delete[] nameList;
system("Pause");
}
Functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "ReadFunctions.h"
#include "SortingAndSearchingFunctions.h"
#endif
ReadFunctions.h
#ifndef READ_FUNCTIONS_H
#define READ_FUNCTIONS_H
#include <iostream>
#include <conio.h>
#include <memory.h>
#include "Functions.h"
using namespace std;
char* readLine();
char** readList(int &);
#endif
ReadFunctions.cpp (the readList function was having problems before, and although errors are not popping up, I still think that there may be a memory leak.)
#include "ReadFunctions.h"
static void backspace() {
_putch('\b');
_putch(' ');
}
char** readList(int & numWords) { // read list of words
// initialize variables
const int firstNumWords = 10;
char* word;
numWords = 0;
int maxWords = firstNumWords;
char** wordList = new char*[maxWords];
char** temp;
// read list loop
while ((word = readLine())[0] != '\0') {
// add word
wordList[numWords++] = word;
// expand list
if (numWords >= maxWords) {
maxWords += firstNumWords;
temp = new char*[maxWords];
memcpy(temp, wordList, numWords * sizeof(char*));
wordList = temp;
//may need delete statement?
}
}
return wordList;
}
char* readLine() { // read word
// initialize variables
const int firstNumChar = 10;
char c;
int charsEntered = 0;
int maxChars = firstNumChar;
char* word = new char[maxChars + 1];
char* temp;
cout << "> ";
// read word loop
while ((c = _getch()) != '\r') {
// backspace
if (c == '\b') {
if (charsEntered == 0) {
c = '\a';
}
else {
backspace();
charsEntered--;
word[charsEntered] = '\0';
}
}
else {
// add character
word[charsEntered++] = c;
// expand word
if (charsEntered >= maxChars) {
maxChars += firstNumChar;
temp = new char[maxChars + 1];
memcpy(temp, word, charsEntered);
delete[] word;
word = temp;
}
}
_putch(c);
}
_putch('\n');
word[charsEntered] = '\0';
return word;
}
SortingAndSearchingFunctions.h
#ifndef SORTING_AND_SEARCHING_FUNCTIONS_H
#define SORTING_AND_SEARCHING_FUNCTIONS_H
#include "Functions.h"
#include <string.h>
void bubbleSort(char** &, int);
int binarySearch(char** &, int, char*);
#endif
SortingAndSearchingFunctions.cpp
#include "SortingAndSearchingFunctions.h"
static bool isAfter(char* str1, char* str2) { // check if first string is after the second string
// initialize variables
bool isGreater = false;
bool isEqual = false;
char c1;
char c2;
int compareLen = (strlen(str1) >= strlen(str2)) ? strlen(str2) : strlen(str1);
// check c-strings
for (int i = 0; i < compareLen; i++) {
// get compare characters
c1 = str1[i];
c2 = str2[i];
// check if both alphabetical
if (((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) && ((c2 >= 'a' && c2 <= 'z') || (c2 >= 'A' && c2 <= 'Z'))) {
// normalize for case insensitive
if (c1 >= 'A' && c1 <= 'Z') {
c1 = (char)(c1 + 32);
}
else if (c2 >= 'A' && c2 <= 'Z') {
c2 = (char)(c2 + 32);
}
}
// handle comes after
if (c1 > c2) {
isGreater = true;
isEqual = false;
i = compareLen;
}
// handle comes before
else if (c1 < c2) {
isGreater = false;
isEqual = false;
i = compareLen;
}
// handle equal
else {
isEqual = true;
}
// case with symbol, which case to compare with?
}
// handle equal comparison region
if (isEqual) {
if (strlen(str1) > strlen(str2)) {
isGreater = true;
}
}
return isGreater;
}
void bubbleSort(char** & wordList, int numWords) { // bubble sort for word lists
// initialize variables
char* temp;
bool swap = false;
// sort loop
do {
// set swap and numwords
swap = false;
numWords--;
// check list
for (int i = 0; i < numWords; i++) {
// swap
if (isAfter(wordList[i], wordList[i + 1])) {
swap = true;
temp = new char[((strlen(wordList[i]) >= strlen(wordList[i + 1])) ? strlen(wordList[i + 1]) : strlen(wordList[i])) + 1];
memcpy(temp, wordList[i], strlen(wordList[i]) + 1);
//temp = wordList[i];
memcpy(wordList[i], wordList[i + 1], strlen(wordList[i + 1]) + 1);
//wordList[i] = wordList[i + 1];
memcpy(wordList[i + 1], temp, strlen(temp) + 1);
//wordList[i + 1] = temp;
//delete[] temp; // if included shows access violation, but should still include and find another way?
}
}
} while (swap);
}
int binarySearch(char** & wordList, int numWords, char* search) { // binary search for word lists
// initialize variables
int first = 0;
int mid;
int last = numWords - 1;
// search loop
do {
// set middle index
mid = (first + last) / 2;
// check if equal
if (strcmp(search, wordList[mid]) == 0) {
return mid;
}
// check if after
else if (isAfter(search, wordList[mid])) {
first = mid + 1;
}
// check if before
else {
last = mid - 1;
}
} while (first <= last);
// not found
return -1;
}
If you don't find any problems, is there any way that I can properly set up dynamic memory and check for memory leaks so that I don't have any worries about this problem.
EDIT: Here's a modified version of the code in the while loop in the readList function in the ReadFunctions.cpp file:
// add word
wordList[numWords++] = word;
// expand list
if (numWords >= maxWords) {
maxWords += firstNumWords;
temp = new char*[maxWords];
memcpy(temp, wordList, numWords * sizeof(char*));
delete[] wordList;
wordList = temp;
}

What's keeping you from using std::vector and std::string? This would eliminate all memory leaks, make the code more efficient, and reduce the size of you code quite a bit. Using std::sort would also help reduce the unnecessary complexity of the code.
And you do need a delete[] statement around the //may need delete statement? comment.
Your modified code doesn't work, because :
if (numWords >= maxWords) {
maxWords += firstNumWords;
temp = new char*[maxWords];
for (int i = 0; i < numWords; i++) { // you're copying into uninititialized pointers !!!!!
memcpy(temp[i], wordList[i], (strlen(wordList[i]) + 1) * sizeof(char));
}
for (int i = 0; i < numWords; i++) {
delete[] wordList[i];
}
wordList = temp;
}
The proper way to resize an array of pointers:
if (numWords >= maxWords) {
temp = new char*[maxWords + firstNumWords]; // allocate new array
memcpy(temp, wordList, maxWords * sizeof(char*)); // copy the pointer values.
char* p = wordList;
wordList = temp; // put new array in place.
delete[] p; // delete old array.
maxWords += firstNumWords; // fix size var.
}

Related

Recursive program to insert a star between pair of identical characters

void pairStar(char input[]) {
int len = strlen(input);
if(input[0] == '\0'){
return ;
}
if(input[0] == input[1]){
for(int i = len; i >= 2; i--){
input[i] = input[i-1];
}
input[2] = input[1];
input[1] = '*';
}
pairStar(input+1);
}
what is the mistake in this code. I couldn't find out.
if I entered aaaa, abaa etc. it runs perfect.
but when i entered scatter is shows scat*ter?#
so what is the perfect solution ?
input string is aaaa . so output is a*a*a*a instead of a*aa*a.
You do not copy terminating character \0!
So when you have inserted a character termination of string is undefined.
Note also that proper answer for aaaa should be a*a*a*a (or a*aa*a depending on problem definition).
Split argument to define soruce and destination and use separate buffers.
void copyAddStarToPairs(const char *src, char *dst)
{
*dst = *src;
if (!*dst) return;
dst++;
char prev = *src++;
if (prev = *src) *dst++ = '*';
copyAddStarToPairs(src, dst);
}
May be you should use std::string as your char buffer may not be long enough.
#include <string>
using std::string;
void pairStar(string & s, int st) {
if (st >= (int)s.size() - 1)
return;
else if (s[st] == s[st + 1]) {
s += ' ';
for (int i = (int)s.size() - 1; i > st + 1; i--)
s[i] = s[i - 1];
s[st + 1] = '*';
st++;
}
pairStar(s, st + 1);
}
int main() {
string s = "aadqwedabbbbb*dqwdasd";
pairStar(s, 0);
cout << s << endl;
}
Add the line input[len+1]='\0'; after the lineinput[1]='*';.
What happens actually is that once you append '*',you aren't appending the '\0' character. This line will append it to the end that means string end here for the first call that you made. So recursive function will also do the same, it will add '\0' character at the end letting know that string ends here.
Consider an example:
aa is a string of length 2 and and after adding star the length of string will become 3 so input[len+1] will be input[2+1]=input[3].
So character next to the moved character will be '\0' character everytime.
PS: this is my first time adding an answer on stackoverflow...
I offer to solve this problem, using the algorithm Depth first search.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int maximumSize=40;
string symbols="aadqwedabbbbb*dqwdasd";
string symbolsNew;
vector<int> visited(maximumSize, 0);
template<class Type>
void showContent(Type input)
{
for(int index=0; index<input.size(); ++index)
{
cout<<input[index];
}
return;
}
void depthFirstSearch(int current, int previous)
{
if(visited[current]==1)
{
return;
}
visited[current]=1;
symbolsNew.push_back(symbols[current]);
int interactions=0;
for(int next=(current+1); next<symbols.size(); ++next)
{
if(interactions!=0)
{
break;
}
if(symbols[current]==symbols[next])
{
symbolsNew.push_back('*');
}
++interactions;
}
for(int next=current; next<symbols.size(); ++next)
{
depthFirstSearch(next, current);
}
return;
}
void solve()
{
depthFirstSearch(0, -1);
cout<<"symbols <- ";
showContent(symbols);
cout<<endl<<"symbolsNew <- ";
showContent(symbolsNew);
return;
}
int main()
{
solve();
return 0;
}
Here is the result:
symbols <- aadqwedabbbbb*dqwdasd
symbolsNew <- a*adqwedab*b*b*b*b*dqwdasd
void pairStar(char input[]) {
int len = strlen(input);
if(input[0] == '\0'){
return ;
}
if(input[0] == input[1]){
for(int i = len+1; i >= 2; i--){
input[i] = input[i-1];
}
input[2] = input[1];
input[1] = '*';
}
pairStar(input+1);
}
void helper(char input[], int start)
{
if (input[start] == '\0')
{
return;
}
helper(input, start + 1);
if (input[start] == input[start + 1])
{
for (int i = strlen(input); i >= start + 1; i--)
{
input[i + 1] = input[i];
}
input[start + 1] = '*';
}
}
void pairStar(char input[])
{
helper(input, 0);
}

Finding anagrams in a text file

#include "iostream"
#include "string"
#include <fstream>
#include <cctype>
using namespace std;
#define NO_OF_CHARS 256
bool checkAnagram(string a,string b);
// Function to print all pairs of anagrams
void findAllAnagrams(string str[], int n)
{
// loop for all strings from array
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (checkAnagram(str[i], str[j])) // check if two strings are anagrams
cout << str[i] << " is anagram of " << str[j] << endl; // print if strings are anagrams
}
// function to check whether two strings are anagram or not //
bool checkAnagram(string string1, string string2)
{
// Create two count arrays and initialize all values as 0
int count[NO_OF_CHARS] = {0};
int i;
for (i = 0; string1[i] && string2[i]; i++)
{
count[string1[i]]++;
count[string2[i]]--;
}
if (string1[i] || string2[i])
return false;
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return false;
return true;
}
int main()
{
string string_name;
ifstream file_stream;
int count = 0;
file_stream.open("PrideAndPrejudice.txt"); // open file to read. your filename
// while loop to count number of strings
while (getline(file_stream, string_name , ' '))
{
count++;
for (int i = 0, length = string_name.size(); i < length; i++)
{
string_name[i] = tolower(string_name[i]);
// check whether parsing character is punctuation or not
if (ispunct(string_name[i]))
{
string_name.erase(i--, 1); // if punctuation is found erase that character
length = string_name.size(); // reduce size of string
}
}
}
file_stream.close();
file_stream.open("PrideAndPrejudice.txt"); // open file again to point to start of file
string wordArr[count]; // create word array to store the list of words read from file
count = 0; // initialize count to 0
while (getline(file_stream, string_name , ' '))
{
for (int i = 0, length = string_name.size(); i < length; i++)
{
string_name[i] = tolower(string_name[i]); // convert uppercase to lowercase
// check whether parsing character is punctuation or not
if (ispunct(string_name[i]))
{
string_name.erase(i--, 1);
length = string_name.size();
}
}
wordArr[count] = string_name; // store word in array
count++;
}
findAllAnagrams(wordArr, count); // check all anagrams
file_stream.close(); // close file stream
return 0;
}
My code is giving no output, am I not reading in the file correctly?
This is part of the complete solution to find anagram between to strings:
/** ASSUMES THE IMPLEMENTATION OF quicksort METHOD TO SORT THE STRINGS.
* return '1' if str1 & str2 are anagrams. '0' otherwise.
*/
int isanagram2(char *str1, char* str2)
{
// If both are NULL. Return true
if(str1 == NULL && str2 == NULL)
return true;
// If one of them is null & other not.
if(str1 == NULL || str2 == NULL || strlen(str1) != strlen(str2))
return false;
quicksort(str1);
quicksort(str2);
// Compare char-by-char
for(int i=0; str1[i] != '\0' ; i++)
if(str1[i] != str2[i])
return false;
return true;
}

Creating a Palindrome String Checker C++ using Arrays and No Pointers

Sorry if this seems really noobish and all, and the fact is, i've only been studying this for about a month now. I can't seem to get this to work right, wondering if any of you can help me out. The program once compiled only says that the inputted words are "not palindrome" even if they are.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
bool isPal (char[], int);
int main ()
{
const int size = 10;
int flag = 0;
char arr[size];
cout << "Enter Word\n";
cin.getline(arr,10);
flag = isPal(arr, size);
if (flag == true)
cout << "Word is Palindrome\n";
else
cout << "Not Palindrome\n";
return 0;
}
bool isPal (char arr [],int size)
{
int q = 0;
char arr2[size];
for (int i = 0;i < size - 1; i++)
{
arr2[i] = arr[9 - q]; //here I attempt to reverse arr1 and assign each element to arr2
q++;
}
for (int j = 0; j < size - 1; j++)
{
if (arr [j] != arr2[j])
return false;
}
return true
}
If you only want to use plain C arrays then check with below code:-
bool isPallindrome ( char *p, int size ) //Remember here size must be one character before null //terminator.
{
char *begin, *end;
begin = p;
end = p + size;
while ( begin != end )
{
if( *begin != *end )
break;
begin++;
end--;
}
if ( begin != end )
return false;
return true;
}
Initialize the arr2
Use strlen to know the length you Input rather than use size, because your input data is not always fixed in 10
In your original code:
input: 1234567
*arr => "1234567\0"
*arr2 => "??\07654321"
Use size replaced by q
bool isPal (char arr [],int size)
{
//q=0; /* modified 3 */
char arr2[size] = ""; /* modified 1 */
for (int i=0; i<strlen(arr); i++) /* modified 2 */
{
arr2[i] = arr[strlen(arr)-i-1]; /* modified 2 */
//q++; /* modified 3 */
}
for (int i=0; i<strlen(arr); i++) /* modified 2 */
{
if (arr[i] != arr2[i])
return false;
}
return true
}
Array is "a pointer point to begin of array address", But for newbie, it's not easy to know how it works. You can reserve this thread until you learn the chapter pointer
Following the example of modified original code:
char arr[size];
char arr2[size];
// char *parr = (char*)malloc(sizeof(char)*size);
// char *parr2 = (char*)malloc(sizeof(char)*size);
cin.getline(arr,10);
// parr = &arr[0];
// cin.getline(parr, 10);
arr2[i] = arr[9 - q];
// parr1 = &arr[9-q];
// parr2 = &arr2[i]
// *parr2 = *parr1;
if (arr [j] != arr2[j])
// parr1 = &arr[j]
// parr2 = &arr2[j]
// if (*parr1 != *parr2)

How can I reverse the words in a sentence without using built-in functions?

This was the interview question:
How to convert Dogs like cats to cats like Dogs ?
My code shows: cats like cats. Where am I making the mistakes?
#include <iostream>
using namespace std;
int main()
{
char sentence[] = ("dogs like cats");
cout << sentence << endl;
int len = 0;
for (int i = 0; sentence[i] != '\0'; i++)
{
len++;
}
cout << len << endl;
char reverse[len];
int k = 0;
for (int j = len - 1; j >= 0; j--)
{
reverse[k] = sentence[j];
k++;
}
cout << reverse << endl;
int words = 0;
char str[len];
for (int l = 0; reverse[l] != '\0'; l++)
{
if (reverse[l] == ' ' || reverse[l] == '\0') // not sure about this part
{
for (int m = l; m >= 0; m--)
{
str[words] = reverse[m];
words++;
}
}
}
cout << str;
return 0;
}
I know you can do this using pointers, stack, vectors... but interviewer was not interested in that!
This is a fixed version of your sample code:
Your principal problem is that every time you found and ' ' or '\0' you copy the bytes of the reverse string from the beginning to that point. Example in loop 5 you copy from index 0-5 (stac) from reverse to str in reverse order, but in in loop 10 you copy from index 0-10 (stac ekil) from reverse to str in reverse order, until here you have already the printed result string ('cats like cats'), and the same in loop 15 all of this incrementing the index of str, in the last loop you are written pass the end of the valid memory of str (and because of that not printed as output).
You need to keep track when end the last word reversed to reverse only the actual word, and not the string from the beginning to the actual index.
You don't want to count the special character (' ' and '\0') in the reversing of the words, you would end with cats like\0dogs
Modified sample code provided:
#include <iostream>
using namespace std;
int main() {
char sentence[] = ("dogs like cats");
cout << sentence << endl;
int len = 0;
for (int i = 0; sentence[i] != '\0'; i++) {
len++;
}
cout << len << endl;
char reverse[len];
int k = 0;
for (int j = len - 1; j >= 0; j--) {
reverse[k] = sentence[j];
k++;
}
cout << reverse << endl;
int words = 0;
char str[len];
// change here added last_l to track the end of the last word reversed, moved
// the check of the end condition to the end of loop body for handling the \0
// case
for (int l = 0, last_l = 0; ; l++) {
if (reverse[l] == ' ' || reverse[l] == '\0')
{
for (int m = l - 1; m >= last_l; m--) { // change here, using last_t to
str[words] = reverse[m]; // only reverse the last word
words++; // without the split character
}
last_l = l + 1; // update the end of the last
// word reversed
str[words] = reverse[l]; // copy the split character
words++;
}
if (reverse[l] == '\0') // break the loop
break;
}
cout << str << endl;
return 0;
}
Some code, written with the restriction of using the most simple features of the language.
#include <iostream>
// reverse any block of text.
void reverse(char* left, char* right) {
while (left < right) {
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main() {
char sentence[] = "dogs like cats";
std::cout << sentence << std::endl;
// The same length calculation as sample code.
int len = 0;
for (int i = 0; sentence[i] != '\0'; i++) {
len++;
}
std::cout << len << std::endl;
// reverse all the text (ex: 'stac ekil sgod')
reverse(sentence, sentence + len - 1);
// reverse word by word.
char* end = sentence;
char* begin = sentence;
while (end < sentence + len) {
if (*end != ' ')
end++;
if (end == sentence + len || *end == ' ') {
reverse(begin, end - 1);
begin = end + 1;
end = begin;
}
}
std::cout << sentence << std::endl;
return 0;
}
Dissecting your algorithm in pieces. First, you find the length of the string, not including the null char terminator. This is correct, though could be simplified.
size_t len = 0;
for (int i = 0; sentence[i] != '\0'; i++) {
len++;
}
cout << len << endl;
This could easily be written simply as:
size_t len = 0;
while (sentence[len])
++len;
Next, you reverse the entire string, but the first defect surfaces. The VLA (variable length array) you declare here, (which you don't need and shouldn't use, as it is a C++ extension and non-standard) does not account for, nor set, a terminating null-char.
char reverse[len]; // !! should be len+1
int k = 0;
for (int j = len - 1; j >= 0; j--) {
reverse[k] = sentence[j];
k++;
}
// !! Should have reverse[k] = 0; here.
cout << reverse << endl; // !! Undefined-behavior. no terminator.
This temporary buffer string is not needed at all. There is no reason you can't do this entire operation in-place. Once we calculate len correctly, you simply do something like the following to reverse the entire sequence, which retains the null char terminator in proper position:
// reverse entire sequence
int i = 0, j = len;
while (i < j--)
{
char c = sentence[i];
sentence[i++] = sentence[j];
sentence[j] = c;
}
Next we move to where you try to reverse each internal word. Again, just as before, the buffer length is not correct. It should be len+1. Worse (hard to imagine), you never remember where you left off when finding the end point of a word. That location should be the next point you start checking for, and skipping, whitespace. Without retaining that you copy from current point all the way back to the beginning of the string. which essentially blasts cats over dogs.
int words = 0;
char str[len]; // !! should be len+1
for (int l = 0; reverse[l] != '\0'; l++)
{
if (reverse[l] == ' ' || reverse[l] == '\0') // not sure about this part
{
for (int m = l; m >= 0; m--) {
str[words] = reverse[m];
words++;
}
}
}
cout << str; //!! Undefined behavior. non-terminated string.
Once again, this can be done in-place without difficulty at all. One such algorithm looks like this (and notice the loop that reverses the actual word is not-coincidentally the same algorithm as reversing our entire buffer):
// walk again, reversing each word.
i = 0;
while (sentence[i])
{
// skip ws; root 'i' at beginning of word
while (sentence[i] == ' ') // or use std::isspace(sentence[i])
++i;
// skip until ws or eos; root 'j' at one-past end of word
j = i;
while (sentence[j] && sentence[j] != ' ') // or use !std::isspace(sentence[j])
++j;
// remember the last position
size_t last = j;
// same reversal algorithm we had before
while (i < j--)
{
char c = sentence[i];
sentence[i++] = sentence[j];
sentence[j] = c;
}
// start at the termination point where we last stopped
i = last;
}
Putting It All Together
Though considerably simpler to use pointers than all these index variables, the following will do what you're attempting, in place.
#include <iostream>
int main()
{
char s[] = "dogs like cats";
std::cout << s << '\n';
size_t len = 0, i, j;
while (s[len])
++len;
// reverse entire sequence
i = 0, j = len;
while (i < j--)
{
char c = s[i]; // or use std::swap
s[i++] = s[j];
s[j] = c;
}
// walk again, reversing each word.
i = 0;
while (s[i])
{
// skip ws; root 'i' at beginning of word
while (s[i] == ' ') // or use std::isspace
++i;
// skip until ws or eos; root 'j' at one-past end of word
j = i;
while (s[j] && s[j] != ' ') // or use !std::isspace
++j;
// remember the last position
size_t last = j;
while (i < j--)
{
char c = s[i]; // or use std::swap
s[i++] = s[j];
s[j] = c;
}
// start at last-left posiion
i = last;
}
std::cout << s << '\n';
return 0;
}
Output
dogs like cats
cats like dogs
My advise would be to break up the original string into an array of words, reverse that array. Then add those words to your reversed sentence with a space in between.
Since they asked for no libraries, I assumed no std::string, no vectors, nothing at all and so I wrote it in C.. the only thing used is printf. Everything else is from scratch :l
The idea is that you reverse the array first. Then split the array by space and reverse each word.
Example: http://ideone.com/io6Bh9
Code:
#include <stdio.h>
int strlen(const char* s)
{
int l = 0;
while (*s++) ++l;
return l;
}
void reverse(char* str)
{
int i = 0, j = strlen(str) - 1;
for(; i < j; ++i, --j)
{
str[i] ^= str[j];
str[j] ^= str[i];
str[i] ^= str[j];
}
}
void nulltok(char* str, char tok, int* parts)
{
int i = 0, len = strlen(str);
*parts = 1;
for (; i < len; ++i)
{
if (str[i] == tok)
{
str[i] = '\0';
++(*parts);
}
}
}
char* reverse_sentence(char* str)
{
char* tmp = str;
reverse(str);
int i = 0, parts = 0, len = strlen(str);
nulltok(str, 0x20, &parts);
while(parts--)
{
reverse(str);
str += strlen(str) + 1;
}
for(; i < len; ++i)
if (tmp[i] == '\0')
tmp[i] = 0x20;
return tmp;
}
int main(void)
{
char str[] = "dogs like cats";
printf("%s", reverse_sentence(str));
return 0;
}
My solution
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str;
cout<<"enter the sentence"<<endl;
getline(cin,str);
char* pch;
pch = strtok((char*)str.c_str()," ");
string rev = "";
while(NULL != pch)
{
rev.insert(0,pch);
rev.insert(0," ");
pch = strtok(NULL," ");
}
cout<<"the reversed string is :"<<rev<<endl;
return 0;
}

how to check whether 2 strings are rotations to each other ?

Given 2 strings, design a function that can check whether they are rotations to each other without making any changes on them ? The return value is boolean.
e.g ABCD, ABDC, they are not rotations. return false
ABCD, CDAB or DABC are rotations. return true.
My solution:
shift one of them to right or left one position and then compare them at each iteration.
If they are not equal at all iterations, return false. Otherwise, return true.
It is O(n). Are there other more efficient solutions ?
What if the contents of them cannot be changed ?
thanks
Concatenate the given string with the given string.
Search for the target string in the concatenated string.
Example:
Given = CDAB
After step 1, Concatenated = CDABCDAB
After step 2, Success CDABCDAB
^^^^
Rather than shifting one of them, it might be more efficient to use two index variables. Start one at 0 each time and the other at each of the possible positions (0 to N-1) and increment it mod N.
If you can't modify the strings, just take the first character of string1 and compare it to each character of string2. When you get a match, compare the second char of string1 to the next char of string2, and so on.
Pseudocode:
len = strlen(string1);
len2 = strlen(string2);
if( len != len2 )
printf("Nope.");
for( int i2=0; i2 < len; i2++ ) {
for( int i1=0; i1<len; i1++ ) {
if( string1[i1] != string2[(i2+i1)%len] )
break;
}
if( i1 == len ) {
print("Yup.");
break;
}
}
A simple one would be:
(s1+s1).find(s2) != string::npos && s1.size() == s2.size();
#include <iostream>
#include <cstring>
#include<string>
using namespace std;
void CompareString(string, string, int);
int ComputeStringLength(string str);
int main()
{
string str = ""; string str1 = ""; int len = 0, len1 = 0;
cout << "\nenter string ";
cin >> str;
cout << "\nenter string 2 to compare:- ";
cin >> str1;
len = ComputeStringLength(str);
len1 = ComputeStringLength(str1);
if (len == len1)
CompareString(str, str1, len);
else
cout << "rotation not possible";
getchar();
return 0;
}
int ComputeStringLength(string str)
{
int len = 0;
for (int i = 0; str[i] != '\0'; i++)
{
len++;
}
return len;
}
void CompareString(string str, string str1, int n)
{
int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
for (int i = 0; i<n; i++)
{
for (int j = flagj; j<n; j++)
{
if (str[i] == str1[j])
{
index = j;
flagj =j;
count1++;
flag++;
if (flag == 1)
{
curr_index = index;
}
break;
}
}
}
int temp = count1;
if (count1 != n)
{
if (curr_index>=0)
{
int k = 0;
for (int i = n - 1; i>n - curr_index - 1; i--)
{
if (str[i] == str1[k])
{
temp++;
k++;
}
}
}
if (temp == n)
{
cout << "\n\nstring is same after rotation";
}
else
{
cout << "\n\nstring is not same after rotation";
}
}
else
{
cout << "\n\nstring is same after rotation";
}
}
https://dsconceptuals.blogspot.in/2016/10/a-program-to-check-if-strings-are.html