0

I'm trying to write a simple program which takes an array of chars, and spits it out backwards. I know there are plenty of other ways to shorten this using a library header function, but I wanted to do it using for loops just to get used to them.

#include<stdio.h>
#include<iostream>
using namespace std;

char string1[10];
int count = 0;
char stringy[10];

void enterString()
{
    cout << "please enter a string: " << endl;
    cin >> string1;
}

void stringCounter(const char stringLength[])
{
    //initiate for loop i = 0
    //if stringLength[i] does not does not equal 'i' then carry on
    //increment i
    for (int i = 0; stringLength[i] != '\0'; i++)
    {   
        count++;
    }   
    cout << "size of string is: " << count << endl;
}

void reverseString(int arraySize, char string2[])
{
    int counter = 0;
    for (int i = arraySize; i >= 0; string2[i--])
    {   
        stringy[counter] = string2[i];
        counter++;
    }   
    stringy[count] = '\0';
    cout << stringy << endl;
}

int main()
{
enterString();
stringCounter(string1);
reverseString(count, string1);
return 0;
}

This is the whole program. The program is failing in function reverseString. I can't work out how to successfully read the last index of the char array string2[] and copy it into the first index of char array stringy.

2
  • 1
    why the string2[i--] instead of just i-- in the for loop? Commented Apr 29, 2016 at 12:06
  • @KarstenKoop I actually have no clue... you're totally correct Commented May 3, 2016 at 14:47

2 Answers 2

1

One, If the user enters a string more than 10 characters long then your enterString() function will access the array out of its bound, at cin>>string1. So better to use getline to make sure you don't read more than what your array can hold.

Two, with your current implementation the reverseString() function will write to the first element of the array with the null terminator character,if the arraySize<=10, and trying to display that string will not show you anything.

This:

 cin >> string1;//will try to access the array out of its bound if user give more than it can hold,i.e 10 characters

 ...

 for (int i = arraySize; i >= 0; string2[i--])
 {   
    stringy[counter] = string2[i];//the first iteration will put the '\0' character as the first elements of stringy
     counter++;
 } 

Should be changed to:

cin.getline(string1,10);//make sure to get not more than 10 characters,including the null terminator

 .....

for (int i = arraySize-1; i >= 0; i--)
{   
    stringy[counter] = string2[i];
    counter++;
} 
Sign up to request clarification or add additional context in comments.

3 Comments

Great, that explains it! Thanks
Hi @bkVnet, can you explain how I would implement getline to make sure I don't read in more chars than my array can hold?
@nrs90 The first argument of getline is the destination where the string that is read will be put, the second argument is the number of characters to read. So,for example, if you have an array named destination which can hold n number of elements then you say cin.getline(destination,n) to read a maximum of n character from the stream which is the maximum your array can hold.
0

There are many mistakes in your program. If this is the exact code you are compiling then it should throw many errors. Following might help.

#include<iostream>

using namespace std;

void reverseString(int , char *);
int stringCounter(const char );

int stringCounter(const char stringLength[])
{
    int count = 0;
    for (int i = 0; stringLength[i] != '\0'; i++)
        count++;
    cout << "size of string is: " << count << endl;
    return count;
}
void reverseString(int arraySize, char string2[])
{
    int counter = 0;
    char stringy[100];
    for (int i = arraySize - 1; i >= 0; i--)
    {
        stringy[counter] = string2[i];
        counter++;
    }
    stringy[counter] = '\0';
    cout << stringy << endl;
}

int main()
{
    char str[] = "string";
    reverseString(stringCounter(str),str);

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.