10/11/2014

String to Array of Char Conversion in C++

This is a simple program for beginners to convert a string into array of char in C++ program. To convert a string into array of char strcpy function is used which exist in stdio.h library.

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++

No comments:

Post a Comment