encode text in C - text

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

Related

Using fread on Linux is returning 0

I have written the code below, but I am getting 0 returned from fread. perror returns success so I guess its working OK. But I dont understand why I am not reading the data written to the file.
int main(int argc, char **argv)
{
FILE *fp;
char wr_buf[4096];
char rd_buf[4096];
int i;
size_t num;
printf("v1\n");
fp = fopen("/run/media/nvme/test", "w+");
if (fp == NULL)
{
printf("FAIL\n");
return -1;
}
for (i=0; i<4096; i++)
{
wr_buf[i] = i;
rd_buf[i] = 0;
}
num = fwrite(wr_buf , 1 , sizeof(wr_buf) , fp);
printf("WR num %d\n", num);
num = fread(rd_buf , 1 , sizeof(rd_buf) , fp);
printf("RD num %d\n", num);
perror("fread");
for (i=0; i<4096; i++)
{
if (wr_buf[i] != rd_buf[i])
{
printf("ERR %x != %x\n", wr_buf[i], rd_buf[i]);
}
}
fclose(fp);
printf("DONE\n");
return 0;
}
Call rewind(fp); between the fwrite and the fread, to seek back to the beginning of the file. To seek to an arbitrary byte offset, use fseek instead of rewind.

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;
}

I am trying to make a math quiz program in C, I have this so far but I cant figure out what is wrong

After the enter the first answer the code crashes.
Also it states that the memory is unsuccessful allocated. How can i fix this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
int i;
srand(time(NULL));
int *num1;
int *num2;
int response;
int *answer;
char *result;
printf("\nMath Quiz\n");
printf("Enter # of problems: ");
scanf("%d", &response);
based on the number of questions the user wishes to take, allocate enough memory to hold question data
num1 = (int *)calloc(response, sizeof(int));
num2 = (int *)calloc(response, sizeof(int));
answer = (int *)calloc(response, sizeof(int));
result - (char *)calloc(response, sizeof(char));
if(num1 == NULL || num2 == NULL || answer == NULL || result == NULL)
{
printf("memory allocation unsucessful\n");
} //end if
for(i=0; i<response; i++)
{
num1[i] = (rand() % 12)+1;
num2[i] = (rand() % 12)+1;
printf("%d * %d = ", num1[i], num2[i]); //somewhere at this point the program messes up
scanf("%d", &answer[i]);
if(answer[i]= num1[i] * num2[i])
{
result[i] = 'c';
}
else
{
result[i] = 'i';
}
} //end for loop
printf("Quiz Results\n");
printf("Question\tYour Answer\tCorrect");
for(i=0; i<response; i++);
{
if(result[i] == 'c')
{
printf("%d * %d\t\t%d\t\tYES",num1[i],num2[i],answer[i]);
}
else
{
printf("%d * %d\t\t%d\t\tNo",num1[i],num2[i],answer[i]);
}
} //end for loop
free(num1);
free(num2);
free(answer);
free(result);
system("pause");
return 0;
} //end main
answer[i]= num1[i] * num2[i]
should read
answer[i] == num1[i] * num2[i]
= is for assignments, == is for comparisons.
and result - (char *)calloc(response, sizeof(char));
should read
result = (char *)calloc(response, sizeof(char));
If there are other problems, you need to be more specific than "the program messes up".
Also, don't cast the return value of malloc or calloc. Read Do I cast the result of malloc? .
Might this be the answer:
result - (char *)calloc(response, sizeof(char));
The '-' should be an '='.

Convert char to TCHAR* argv[]

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;
}

Resources