C/C++ :: Reversing A String / Char Array

Aug 12, 2013

I'm new in the C programming language, so I tried to create a program that reverses a string. This is my code:

/* Reverse string */
#include <stdio.h>
#include <string.h>  
int main() {
  char s[8]="Welcome";  
 
[Code] ....

The output of the program is "@".

View 5 Replies


ADVERTISEMENT

C++ :: Char Array Reversing Function Using Pointer?

Feb 4, 2014

Why does this code doesnt work?

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string {
char* ptr;

[code] ....

View 1 Replies View Related

C++ :: Reversing A String

Feb 26, 2015

I have this bit of code that reverses a string but there's a bit of it that I just don't understand:

int main()
{
char sentence[20]; //Create char array
void reverse( const char * const sPtr );
printf( "Enter your text please:
" );

[code]....

I just don't get how this bit results in the entered string being reverse.how to make code look like code.

View 6 Replies View Related

C :: Reversing A String Using Pointers

Nov 13, 2013

everything seems to be right in this program except that the puts() function does not print anything.

Code:

/*c program to reverse a string using pointer*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
}

[code]....

View 4 Replies View Related

C :: Reversing String Not Words

Oct 2, 2014

I am trying to write a program in C to reverse a string .

Example - This is America
output - America is This

I have thought of two ways of doing this question

1) using 2D arrays ( no pointer)
2) Using Strings and pointer

Code:
#include<stdio.h>
#include<string.h>
main()
}

[code]....

View 4 Replies View Related

C++ :: Reversing N Characters In A String?

Dec 26, 2013

I have a string like str="ABCDEFGHIJK";

need o/p like this str="CBAFEDIHGJK"

am getting "CBA" correctly after that its not printing anything.

int main()
{
string str="ABCDEFGHIJK";
char str1[10],rev[10];
int n=str.length(),count=0,c=3,k=0,j=0;

[Code].....

View 10 Replies View Related

C++ :: Reversing String Using Seek / Tell?

Aug 8, 2012

I have an assignment that asks me to write a program which reads all lines from a text file (one by another), reverses them and write to the same file. I also have a pseudocode with which I have to work and it is as follows:

While the end of the file has not been reached

pos1 = current get position
Read a line.
If the line was successfully read
pos2 = current get position
Set put position to pos1.
Write the reversed line.
Set get position to pos2.

I have tried many things and the code misses letters. Below you can see my code:

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
fstream file;
file.open("output.txt");

[code]....

This is the text with which I have to work:

Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.

And this is what I get:

bmal elttil a dah yraM
Itswons sa etihw saw eceelf
Antnew yraM taht erehwyreve d
T.og ot erus saw bmal eh

View 7 Replies View Related

C/C++ :: Reversing String Loop - Compiler Error

Feb 13, 2014

I need to reverse this loop. get how to do it in order but when i have to reverse it i get a compiler error

int main() {
cout << "Enter 3 cities" << endl;
string cities;
for ( int i = 0; i < 3; ++i ) {
getline(cin, cities[3];

[Code] ....

View 2 Replies View Related

C++ :: Char Array To String - String Becomes Garbage

Apr 20, 2013

I'm trying to "tokenize" a string using std::string functions, but I stored the text in a char array. When I try to convert it to a string, the string has the first character right but the rest is garbage.

// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessage(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);

[Code] ....

View 3 Replies View Related

C :: Reversing Elements In Array

Apr 15, 2014

Write the definition of a function reverse , whose first parameter is an array of integers and whose second parameter is the number of elements in thearray . The function reverses the elements of the array . The function does not return a value .

Code:
void reverse(int a[], int num) {
for ( int i=0; i <= num/2 ; i++){
int temp = a[i];
a[i] = a[num-i-1];
a[num-i-1] = temp;
} }

This is supposed to be the answer but I'm not quite sure why this is. I understand everything up until the actual loop. For one, shouldn't "int i" be declared outside the loop (I thought perhaps this was an error in the solutions)?

The main thing that I do not understand is the conditional statement.

Code: i<=num/2;

I don't understand why the "num/2" is necessary here. Also I can't really remember but is there a command that actually reverses an array?

View 5 Replies View Related

C/C++ :: Reversing Array Contents

Feb 21, 2015

I'm trying to reverse 25 items that are in my array by using a For Loop and sending 2 of the numbers to a function at a time. The code I have right here, is not working at the moment, I cannot find the problem because i've been staring at my code for too long.

int farEnd = 24;
for (int i = 0; i < 24; i++){
Swap(intArray[i], intArray[farEnd]);
farEnd--;
}

Also here is the code in my function

void Swap(int num1, int num2){
int temp = 0;

temp = num1;
num1 = num2;
num2 = temp;
}

View 5 Replies View Related

C++ :: Char Array To String

Oct 19, 2013

I have some code:

char cHomeTeamFaceOffsPercentageWon[100];
memcpy(cHomeTeamFaceOffsPercentageWon,cSqlHomeTeamFaceOffsPercentageWon,100);

After this, for example, cHomeTeamFaceOffsPercentageWon is, "29%".

Then, I use

std::string szwPercentageWon = std::string(cHomeTeamFaceOffsPercentageWon);

szwPercentageWon is then, "2". Shouldn't this convert correctly, to "29%" as well.

Or is there something I'm missing? Maybe the termination character, or something.

View 1 Replies View Related

C/C++ :: Reading A File Into Array Then Reversing The Array

Jan 19, 2015

This is what I am supposed to be doing with this problem: Read the integer values in the file "numbers.dat" and display

* them in reverse order (that is, display the last number in the
* file first, the second-to-last second and so on). Use an array to
* store the values. It is safe to assume that the file contains no
* more than 100 values.

I have gotten far enough to read the file and display as an array, but it is displaying vertically rather than horizontally. So it is displaying 10 and a 1 on the first line then the 0 on the second line. Before I can work the reverse part out, I need it to display each number as 10 line 1 -235 line 2 so on and so forth.

This is my code so far:

void display_array_reversed() {
ifstream fin ("numbers.dat");
if (!fin){
cerr << "Error opening file" << endl;
exit(1);

[Code] .....

View 2 Replies View Related

C++ :: Converting String To Char Array

Mar 30, 2014

In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.

ReadNew function reads the file....check to see if it exist

CreateNew function creates a new file.

In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions. Here is my code.

Code:

//Create a file, append to it, and read it.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);

[Code]...

View 1 Replies View Related

C :: Convert Char Array To String

Oct 15, 2013

Currently I have:

Code:
char my_array[4] = { '1', '2', '3', '4' };

how do I convert the above to a string of: "1234"

View 6 Replies View Related

C :: How To Convert A String Into 2D Char Array

Mar 8, 2013

.I have this string that I need to convert into a 2d char array like this:

String str= "A,E,B,I,M,Y#N,R,C,A,T,S";

I know how to use delimiter and split string and I know how to convert but only from string to char[].I need something like this:

Input: String str= "A,E,B,I,M,Y#N,R,C,A,T,S";

Output: [A] [E] [B] [I] [M] [Y][N] [R] [C] [A] [T] [S]

View 6 Replies View Related

C++ :: Assign Value Of Pow (2,800) To Char Array Or String

Jan 31, 2015

Assign value of pow(2,800) to char array or string ....

View 1 Replies View Related

C/C++ :: Testing String Against Array Of Char

Apr 3, 2013

I have a text file with state names, and state abbreviations, thusly:

ALASKA
AK
ARKANSAS
AR
..and so on.

I have to load the abbreviations ONLY from the file into an array of char[ - (already done and tested).

I have to get a 2 char abbreviation as a string,then test it against the state array to make sure it is a valid abbreviation. As it stands, my test is never finding an invalid abbreviation..

Here is where I get the input:

void getState() {
char state[10];
getString("Please enter the state as a 2 char abbreviation:",state,10);
printf("State Entered:%s", state);
validState(state);

[Code] ....

View 14 Replies View Related

C/C++ :: Convert String To A Char Array?

Apr 29, 2015

How do I convert a string of unknown length to a char array? I am reading strings from a file and checking if the string is a Palindrome or not. I think my palindrome function is correct, but do I do something like char [] array = string.length(); ??

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

[Code].....

View 2 Replies View Related

C :: Change Elements Of Char String Array

Dec 16, 2013

why does this give me an error when i try to change the elements of the char string array.

code:

int main(void)
{
char *name = "aaa";
// setup the name
name[0] = 'Z';
name[1] = 'e';
name[2] = 'd';
name[3] = '';

return 0;
}

[code]....

View 3 Replies View Related

C++ :: Assign String To Char Array Of Struct

May 6, 2013

#include <iostream>
using namespace std;
struct box{

[Code].....

C++Dev.cpp:23: error: incompatible types in assignment of ‘const char [15]’ to ‘char [40]’

View 2 Replies View Related

C++ :: Using Isalpha With String Converted To Char Array

Jan 26, 2014

I am new to C++ and I have a two player word guessing game working well. However, I would like to be able to validate whether the word entered by player 1 is a completely alphabetic word using isalpha.

The error I am getting right now is as follows:

"error: array must be initialized with a brace-enclosed initializer
char str[100]=hiddenwordtwo;"

/* isalpha portion of code */
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;

int main () {
char hiddenwordtwo[100];

[Code] .....

View 2 Replies View Related

C# :: Store Each Char Of A String Into Byte-array?

Apr 25, 2014

I am trying to store each char of a string(string a ="1100") into a byteArray ( byte[] byteArray = new byte[4]. its not showing any error but its storing like below:

byteArray[0] = 49
byteArray[1] = 49
byteArray[2] = 48
byteArray[3] = 48

and what i want is

byteArray[0] = 1
byteArray[1] = 1
byteArray[2] = 0
byteArray[3] = 0

I don't know why but its replacing 1 with 49 and 0 with 48.what am I doing wrong or how to do this?

my code is as below

byte[] byteArray = new byte[4];)/>
int binArrayAdd = 0;
string a ="1100";
foreach (char Character in a)
{
byteArray [binArrayAdd] = Convert.ToByte(Character);
binArrayAdd++;
}

View 4 Replies View Related

C/C++ :: Passing String Variable To Char Array?

Mar 30, 2014

I have program where i have to check to see if file exist, if it does not then it needs to be created. SO I have a read file that works fine, if th efile exist it reads whats in it, if it does it says the file does not exist. Now Im trying to creata function that creates the file if it doesnt exist. so in my read function when the person enters the name of the file to be checked for..I pass that name to a variable called name..Hoping that I could then pass it into my create file function if it does not exist..and use that variable to pass the name they entered into the createfile array..called filename.. but I am having trouble because i get error when i try to pass from a string name to char array.. even when I change the varialbe name to char, or char [256] it will not work.. I try to fing a way to convert th string to a char using the strncopy function but still no dice..here the code i have for the createfile funciton

void CreateNew(ofstream & FileNew)//Create file
{
char filename[256];
string name;

[Code].....

How can i do this without having to ask the person to enter the file name twice..

View 12 Replies View Related

C Sharp :: How To Take String Input Into Char Array

Sep 24, 2014

I want to take string input into a char array.

What is the functionality for the above problem.

View 1 Replies View Related

C++ :: Why Cannot Copy String Pointed By Pointer To Array Of Char

Feb 22, 2013

I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.

private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];

[Code].....

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved