Convert char to TCHAR* argv[] - visual-c++

How can I input text into TCHAR* argv[]?
OR: How can I convert from char to TCHAR* argv[]?
char randcount[] = "Hello world";
TCHAR* argv[];
argv = convert(randcount);

One way to do is:
char a[] = "Hello world";
USES_CONVERSION;
TCHAR* b = A2T(a);

/*This code did TCHAR in my project without A2T or any other converters. Char text is a some kind of array. So we can take letters one by one and put them to TCHAR. */
#include <iostream>
TCHAR* Converter(char* cha)
{
int aa = strlen(cha);
TCHAR* tmp = new TCHAR[aa+1];
for(int i = 0; i< aa+1; i++)
{
tmp[i]=cha[i];
}
return tmp;
}
int main()
{
char* chstr= new char[100];
chstr = "char string";
TCHAR* Tstr = new TCHAR[100];
//Below function "Converter" will do it
Tstr = Converter(chstr);
std::cout<<chstr<<std::endl;
std::wcout<<Tstr<<std::endl;
}

Related

Weird shapes as output( Strings)- C language

So i have a problem. I have to separate the first name, last name and hostname of email.
For example:
zephyr.extreme#gmail.com>> Input
Output=
First name= Zephyr
Last name= extreme
Host Name= gmail.com
I am not getting the desired result. I am getting some weird shapes as output.
Code:
#include <stdio.h>
int main()
{
char email[40], first[20],last[20],host[30];
printf("Enter the email= ");
gets(email);
int i;
while(email[i]!='\0')
{
while(email[i]!='.')
{
first[i]=email[i];
i++;
}
while(email[i]!='#')
{
last[i]=email[i];
i++;
}
while(email[i]!='\0')
{
host[i]=email[i];
i++;
}
}
puts(first);
puts(last);
puts(host);
}
Assuming the format will always be first.last#host..., use this code:
#include <stdio.h>
#include <string.h>
int main()
{
char email[40], first[20],last[20],host[30],name[40];
int firstDot,atSymbol;
int i;
int length;
char *token;
printf("Enter the email= ");
gets(email);
length = strlen(email);
for(i=0;i<length;i++){
if(email[i]=='.')
{
firstDot = i;
}
else if(email[i]=='#')
{
atSymbol = i;
}
}
strncpy(name,email,atSymbol);
name[atSymbol]= '\0';
token = strtok(name, ".");
/* walk through other tokens */
while( token != NULL )
{
printf( "%s\n", token );
token = strtok(NULL, ".");
}
strncpy(host,email+atSymbol,length-atSymbol);
host[length-atSymbol] = '\0';
puts(host);
}
So i updated the code, now the only problem is the last output.
After host name= gmail.com prints, but then some extra shapes are also printing. These are smile face and some weird symbols.
Code:
#include <stdio.h>
int main()
{
char email[40], first[20],last[20],host[30];
printf("Enter the email= ");
gets(email);
int i=0,j;
while(email[i]!='.')
{
first[i]=email[i];
i++;
}
i=0;
while(email[i]!='#')
{
last[i]=email[i];
i++;
}
j=i;
i=0;
while(email[j]!='\0')
{
host[i]=email[j];
j++;
i++;
}
printf("First Name= ");
puts(first);
printf("Last name= ");
puts(last);
printf("Host name= ");
puts(host);
}
C strings (char pointers) should be null-terminated. This means your string needs a '\0' character at its end so that string manipulation functions such as puts or strlen know where they end, in constrast to other languages where the string's length is stored together with it. The "weird shapes" you are seeing are just random data stored after the end of the string being interpreted as characters. When you call puts it just keeps outputting bytes-as-characters until it randomly finds a byte with value '\0'
You can solve this by adding a '\0' character to the end of the string after each of the blocks where you write a string.
while(email[i]!='.')
{
first[i]=email[i];
i++;
}
email[i] = '\0'; //same thing as email[i] = 0; but using a char makes what
//you're doing clearer

Function won't reverse strings properly

#include <iostream>
#include <cstring>
using namespace std;
void reverseString(char s[])
{
int length = strlen(s);
for (int i = 0; s[i] != '\0'; i++) {
char temp = s[i];
s[i] = s[length - i - 1];
s[length - i - 1] = temp;
cout << s[i]; //this ends up printing "eooe" instead of reversing the whole string
}
}
int main()
{
char a[] = "Shoe";
reverseString(a);
return 1;
}
I'm wondering where the algorithm messes up and what I can do to fix it, maybe I overlooked something because when I try to solve it on a piece of paper it appears to work correctly.
Your algo is right but need a little modification, you have to run algorithm for length/2 times. It prevents your string to again swap the contents i.e At i = 2 your s = eohs but it again swaps h with o. Try to insert the break point to understand it further. I modify your function little bit.
char* reverseString(char s[])
{
int length = strlen(s);
for (int i = 0; i<length/2; i++)
{
char temp = s[i];
s[i] = s[length - i - 1];
s[length - i - 1] = temp;
//cout << s[i]; //this ends up printing "eooe" instead of reversing the whole string
}
return s;
}
int main()
{
char a[] = "Shoe";
cout<<reverseString(a);
system("pause");
return 1;
}
Use the code below:
#include <stdio.h>
void strrev(char *p)
{
char *q = p;
while(q && *q) ++q;
for(--q; p < q; ++p, --q)
*p = *p ^ *q,
*q = *p ^ *q,
*p = *p ^ *q;
}
int main(int argc, char **argv)
{
do {
printf("%s ", argv[argc-1]);
strrev(argv[argc-1]);
printf("%s\n", argv[argc-1]);
} while(--argc);
return 0;
}

How to convert between char* and Platform::String^?

How do I convert a string from char* to Platform::String^ and vice versa?
I'm developing a DLL for the Universal Windows Platform, using version 10.0.10586.0 of the SDK and Visual Studio 2015 Update 1.
Not the most elegant but the only solution that worked for me to get const char * from the Platform::String
const char * StringToChar(String^ s) {
const wchar_t *W = s->Data();
int Size = wcslen(W);
char *CString = new char[Size + 1];
CString[Size] = 0;
for (int y = 0;y<Size; y++)
{
CString[y] = (char)W[y];
}
return (const char *)CString;
}
and its a lot easier to convert it back
String^ CharToString(const char * char_array) {
std::string s_str = std::string(char_array);
std::wstring wid_str = std::wstring(s_str.begin(), s_str.end());
const wchar_t* w_char = wid_str.c_str();
return ref new String(w_char);
}
//Char to String
char *text = "new string";
Platform::String str = new Platform::String(text, strlen(text));
//String to char
char16 *newText = str.Data();
More detailed answer: https://stackoverflow.com/a/11746252/5477130

How to change the value of string from a cout

void extractWord (string& str)
I have to write a function that extracts the word between ‘*’.
For example, using the three test cases below:
string s = "This is to be *reversed*"
string s1 ="*Reversed* starts here";
string s2 = "This is *in* the middle";
and after each function call,
s=reversed, s1=Reversed, s2=in
So i figured out...
void extractWord (string& str)
{
char target= '*';
int idx1=0;
while (str[idx1] != target)
idx1=idx1+1;
int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;
for(int i=0;i<sizeof(str);i++)
{
if ((i>idx1)&&(i<idx2))
cout<<str[i];
}
}
int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
extractWord(s);
cout<<endl;
extractWord(s1);
cout<<endl;
extractWord(s2);
cout<<endl;
}
but how do I change the value of s into the output of this function?
I have modified your code a bit. I hope this solves your problem:
//#include "stdafx.h"
#include < string >
#include < iostream >
using namespace std;
void extractWord (string& str)
{
char target= '*';
int idx1=0;
while (str[idx1] != target)
idx1=idx1+1;
int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;
str=str.substr(idx1+1,idx2-idx1-1); //changed
}
int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
extractWord(s);
cout<<s<<endl; //changed
extractWord(s1);
cout<<s1<<endl; //changed
extractWord(s2);
cout<<s2<<endl; //changed
system("PAUSE");
return 0;
}
Now, every time you call void extractWord (string& str) it replaces string with only word between *'s.
I've used std::string::substr function. > http://www.cplusplus.com/reference/string/string/substr/
Other option is to make function that returns word between *'s.
//#include "stdafx.h"
#include < string >
#include < iostream >
using namespace std;
string extractWord (string& str)
{
char target= '*';
int idx1=0;
while (str[idx1] != target)
idx1=idx1+1;
int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;
return str.substr(idx1+1,idx2-idx1-1);
}
int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
string res;
res=extractWord(s);
cout<<res<<endl;
res=extractWord(s1);
cout<<res<<endl;
res=extractWord(s2);
cout<<res<<endl;
system("PAUSE");
return 0;
}
However, note that it doesn't work if your string do not contain two *'s or it has more than one word that has to be extracted. I hope this helped you.

encode text in C

my program reads several string from standard input. I want to encode it like this: where is A print 00,where is B print 01. This is my code. I don't know where I'm wrong. Thank you!
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void)
{
char text[100];
printf("enter text:");
fgets(text,100,stdin);
int i,j;
unsigned int aux;
char a[100];
char b[100];
for(i=0;i<100;i++)
for(j=0;j<100;j++)
{
if(text[i]=='a'){
aux=aux|0;
aux=aux<<2;
a[j-1]=aux;
a[j]='\0';
}
if(text[i]=='b'){
aux=aux|1;
aux=aux<<2;
b[j-1]=aux;
b[j]='\0';
}
strcat(a,b)
}
printf("%s", a[j]);
getch();
return 0;
}
printf ("%02d\n", toupper(text[i]) - 'A');
or
for (i = 0; i < strlen (text); i++)
sprintf (&a[i*3], "%02d ", toupper(text[i]) - 'A');
Note that this only works for text only strings

Resources