Not able to convert string into a char array

raja

I am trying to convert a string with spaces into an array of char without spaces.

Here is what I tried

string str;
    cout << "Enter a string: ";
    getline(cin, str);
    int TempNumOne=str.size();
        char Filename[100];
        for (int a=0;a<=TempNumOne;a++)
                {
                    cout<<str[a]<<endl;
                    if(str[a]!=' ')
                        Filename[a]=str[a];
                }

        cout<<Filename;

The output looks like this

Enter a string: hello world
h
e
l
l
o

w
o
r
l
d

hello

Only hello is getting stored. Why is this happening and how do I solve this?

Sam Varshavchik

This is because:

  if(str[a]!=' ')
          Filename[a]=str[a];

In "hello world", str[4] is o, and str[6] is w. The above code will: store the o into Filename[4] and the w into Filename[6], instead of Filename[5]. A computer only does what you tell it to do, and now what you want it to do. Your logic did skip over the space, but all it did is that it didn't copy it into the corresponding position into the Filename buffer. This is not enough to accomplish the given task.

To do this correctly you need to maintain a separate index variable, call it b, for example, initialize it to 0, then:

 if(str[a]!=' ')
      Filename[b++]=str[a];

and don't forget to null-terminate the Filename, at the end.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to convert a char array back to a string?

From Dev

Convert char to string in OCaml

From Dev

Convert NSString to char array

From Dev

How to Convert a String Array to a Char * Array c++

From Dev

Convert string vector to char array in c++

From Dev

Convert input string into a char array

From Dev

How do I convert an unsigned char array into a string in C?

From Dev

Convert String^ to const char*

From Dev

Convert keycode to char/string

From Dev

cgo - How to convert string to C fixed char array

From Dev

what is the best way to convert a char array (without null termination) to string

From Dev

Convert c++ char array to c# string

From Dev

Not able to convert a string to XML

From Dev

What is the fastest way to convert unsigned char array to IP string

From Dev

c# convert string array to 2 dimensional char array

From Dev

How to convert char* array into std::string

From Dev

Not able to convert response into array

From Dev

How do I convert an unsigned char array into a string in C?

From Dev

Convert keycode to char/string

From Dev

Convert array with 8bit value to string with char (no charcode)

From Dev

Convert char literal in string to char

From Dev

Convert String to 2D Char Array

From Dev

Not able to convert string to decimal in mvc

From Dev

C++ How to convert char pointer array to string array?

From Dev

Convert string to char in python

From Dev

not able to convert char to string with bool char method

From Dev

How to convert char array into string for DateTime.Parse?

From Dev

How to convert char array to string

From Dev

Not able to convert the string to date on Android

Related Related

HotTag

Archive