Monday 10 November 2014

String to array of char conversion in C++

Structure of the Problem Requirements 

In this problem we will learn how to convert a string to array of char in C++ program. To convert a string into array of char strcpy function is used which exist in stdio.h library. Here is the source code of this conversion which help you in better understanding.

Source Code

#include<iostream>
#include<string.h>
#include <stdio.h>
using namespace std;
int main ()
{
cout<<"\t \t \t LEP Tutorials \n \n \n ";
char words [100];
char choice;
string sentence = "WELCOME TO C++ Tutorials \n";
cout<<sentence<<endl;
cout<<"Do You Want to Convert it in Array of Char ? Y/N \n";
cin>>choice;
if ((choice =='Y') ||(choice=='y'))
{
       strcpy(words, sentence.c_str());
       for (int i=0;i<strlen(words);i++)
    {
    cout<<words[i];
    }
}
}

Output of the Program

String to Array of Char Conversion in C++
String to Array of Char Conversion in C++

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment