Juxtaposing integer ASCII codes for alphabet - arrays

I want to convert Name and Surname(e.g., Nova Stark) into a large integer by juxtaposing integer ASCII codes for alphabet, print the corresponding converted integer then cut the large integer into two halves and add the two halves.Following is my approach:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char* arr2str(int arr[], int size) {
static char buffer[256];
memset(&buffer[0], 0, sizeof(buffer)/sizeof(char));
char *ptr = &buffer[0];
for(int i=0; i<size; ++i) {
sprintf(ptr += strlen(ptr), "%d", arr[i]);
}
return buffer;
}
int arr2int(int arr[], int size)
{
char buffer[256] = {0,};
char *ptr = &buffer[0];
for(int i = 0; i < size; ++i) {
sprintf(ptr += strlen(ptr), "%d", arr[i]);
}
return atoi(&buffer[0]);
}
int main()
{
int *A;
long long int num;
int div,base=10;
char name[50],asc[200];
printf("Enter your name : ");
scanf(" %[^\n]",name);
int len=strlen(name);
A=(int*)malloc(len*sizeof(int));
for(int i=0;i<len;i++)
{
A[i]=name[i];
}
char *str = arr2str(A, len); //for converting array to string
num = arr2int(A, len); //again for converting the character array to integer.
//num=array_to_num(A,len);
div=base;
while(num/div>div)
{
div=div*base;
}
long long int a=num/div;
long long int b=num%div;
long long int c=a+b;
printf("The required integer is %lld and the sum is %lld ",num,c);
return 0;
}
But I am not getting the desired output. Please help!
Also, if there exists a simpler approach to the problem please specify that too.

Following is running, for explanations see comments in the code. Maybe you have to handle the whitespaces in the name:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *A;
long long int num;
int div,base=10;
char name[50];
printf("Enter your name : ");
scanf(" %[^\n]",name);
int len=strlen(name);
A=(int*)malloc(len*sizeof(int));
for(int i=0;i<len;i++)
{
*(A+i)=(int)name[i]; // *(A+i) is accessing array as pointer+index : https://www.programiz.com/c-programming/c-dynamic-memory-allocation
// letters to ascii is done by simply typecasting to (int)
}
// string to int array
for(int i=0;i<len;i++)
{
printf("%i -> %c \n", *(A+i), (char)(*(A+i)));
}
// long long int from concatenating the elements of the int array
int s_idx=0; // index for string
char str[512]; //string of fixed size, possibly malloc this
for (int i=0; i<len; i++)
s_idx += snprintf(&str[s_idx], 512-s_idx, "%d", *(A+i));
printf("%s \n", str);
num = strtoll(str, NULL, 10); // https://en.cppreference.com/w/c/string/byte/strtol
// from here your code is unchanged
div=base;
while(num/div>div)
{
div=div*base;
}
long long int a=num/div;
long long int b=num%div;
long long int c=a+b;
printf("The required integer is %lld and the sum is %lld \n ",num,c);
free(A);
return 0;
}

Related

How can I write the total value at the beginning

#include <stdio.h>
int main() {
int k;
unsigned long long int aray[94];
aray[0]=0;
aray[1]=1;
unsigned long long int total=0;
printf("\n FIBONACCI : \n\n");
printf("1 + ");
for(k=2;k<=93;k++){
aray[k]=aray[k-1]+aray[k-2];
total+=aray[k];
printf("%llu + ",aray[k]);
}
return 0;
}
Hi guys I need to print total value printf("%llu",total);at the begining but I don't know how to do this thanks
calculate and "print" into a large enough string (or realloc() as needed)
char bigenough[200000];
char *p = bigenough;
loop {
/* ... calculate ... */
p += sprintf(p, "%llu + ", value);
}
afterwards print total and result string
printf("total: %llu = ", total);
printf("%s\n", bigenough);

Finding all suffix starting with a character X

I need to find all suffix starting with a character X. For example, for int suffix (char str [], char c) when the word is ababcd and the letter b it should return:
babcd
bcd
and the number 2.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[128];
int counter=0;
printf ("Please enter charachter and a string \n");
scanf("%c %s",&c,str);
counter = my_suffix(str,c);
printf("The string has %d suffix \n",counter);
return 0;
}
int my_suffix(char str[],char c) {
int counter = 0;
for (int i=0; i < strlen(str); i++)
{
if (str[i] == c)
{ puts(str+i);
counter++;
}
}
return counter;
}
I couldn't find why it's not running,
Thanks!
Your code is fine you should just written following method above int main()
int my_suffix(char str[],char c){...}

String to int array

I've worked on this for awhile but I keep getting either {0,0,0,0,0}, or {2751685, 2751685, etc} or {57,58,59,60,etc}
void getGuess(int guess[], int length) {
char thisGuess[length];
int i=0;
printf("Enter your guess.\n");
scanf("%s", &thisGuess);
for(i=0; i<length; i++) {
printf("the guess = %d\n",(int)thisGuess[i]) ;
guess[i] = (int)(thisGuess)-48;
printf("%d ", guess[i]);
}
}
I want to enter a string, 12345, and get it so
guess[0] = 1
guess[1] = 2
guess[2] = 3
etc
suggestions on my code?
The length of an char array usually isn't equal to the length of the C-style string it contains.
void getGuess(int guess[], int length) {
char thisGuess[length];
size_t i = 0;
printf("Enter your guess.\n");
scanf("%s", thisGuess); // a char *, rather than a char (*)[length] is expected
size_t guessLength = strlen(thisGuess);
for(i = 0; i < guessLength; i++) {
printf("the guess = %d\n", thisGuess[i]);
guess[i] = thisGuess - '0';
printf("%d ", guess[i]);
}
}
Alternatively, add
if(!isdigit(thisGuess[i]))
continue;
to avoid potential out-of-bound access.

Error: too few arguments to function 'strcmp'

I am having a few issues with my code. First: when I try to compile, I get error: too few arguments to function 'strcmp'. I have looked all over and made multiple changes and am still unable to get it to work. Second: when my code does compile (if I remove the strcmp part), it will not complete the count functions correctly. Can anyone please assist? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(char array[], int size);
int stringLen(char array[]);
int convert(char ch);
int value, n;
int main()
{
//char * str;
//char s;
char a[100];
char b[100];
char c[100];
int charCount = stringLen(a);
int lCount = count(a, charCount);
printf("Enter your string: \n");
scanf("%s \n", a);
printf("Enter your string: \n");
scanf("%s \n", b);
printf("Enter your string: \n");
scanf("%s \n", c);
printf("The count is %d, length is %d\n", lCount, charCount);
int i;
for(i = 0; i < charCount; i++)
{
char c = a[i];
printf("Char %s = %d \n", &c, value);
}
n = strcmp(char string1[], char string2[], char string3[]);
printf("The first string in the alphabet is: %d \n", n);
return 0;
}
int stringLen(char array[])
{
char count;
int index;
while(array[index] !=0)
{
count++;
index++;
}
return count;
}
int count(char array[], int size)
{
int count;
int i;
for(i = 0; i < size; i++)
{
if(array[i] == 'a')
{
count ++;
}
else if(array[i] == 'A')
{
count ++;
}
}
return count;
}
This is not right way to use strcmp.
n = strcmp(char string1[], char string2[], char string3[]);
strcmp is used for compararison of string. See doc
int result = strcmp (string1,string2)
If strings are same, function will return 0.

Recursive generation of letters combinations string

I'm trying to write a recursive program that makes/prints a string with each of all the possible combinations of vowels(A, E, I, O, U), that includes at least once every vowel. The number of elements is taken as input from the user (so to include each vowel at least once >=5). I really can't figure out the right way to do this.
Here's my take on it (not working though):
#include <stdio.h>
#include <stdlib.h>
int voc_gen(int n, char *array);
char rand_voc();
int main()
{
char *array, voc[] = "AEIOU";
int i, N;
printf("Insert number of elements for vocals string:\n");
scanf("%d", &N);
array = malloc(N * sizeof(char));
for(i=0; i<5; i++)
array[i] = voc[i];
voc_gen(N-5, array);
return 0;
}
int voc_gen(int n, char *array){
if(n==0)
return 0;
else{
array[voc_gen(n, array)] = rand_voc();
printf("%s\n", array);
return voc_gen(n-1, array);
}
}
char rand_voc(){
char array[] = "AEIOU";
int i;
return array[rand()%5];
}

Resources