Can I convert float array to string?I dont care if the dot would be as a string value. i just need to separate the numbers into string.
so far I have done this
void H(float *suma, int k){
int i=0;
char str[200] = "";
sprintf(str, "%.2f", *suma);
for(i=0;i<strlen(suma);i++) {
printf("%c", str[i]);
}
}
but it keeps converting only the 1st value in my float array.I hope I made it clear. If not here is an example of my problem.
array[0]= 123.45;
array[1]= 543.21;
but i need it as
string[0]='1';
string[1]='2';
string[2]='3';
string[3]='.';
string[4]='4';
string[5]='5';
string[6]='5';
string[7]='4';
etc...
If you want to print the list of all array numbers in their string format here is the solution (I have assumed k is the length of float array):
void H(float *suma, int k){
int i=0, j;
char str[200] = "";
for(j=0;j<k;j++){
sprintf(str, "%.2f", *(suma+j));
for(i=0;str[i]!='\0';i++) {
printf("%c\n", str[i]);
}
}
}
You can use strcat() to concatenate strings if you want all of them in a single string.
Here's a solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void H(float *suma, int k)
{
int i,j;
char str[200] = "";
for(j=0; j<k; j++)
{
sprintf(str, "%.2f", suma[j]);
for(i=0;i<strlen(str);i++)
{
printf("%c", str[i]);
}
printf("\n");
}
}
int main ()
{
float b[]={123.432,213.432,12.2,31.3,13.4};
H(b,5);
system("pause");
return 0;
}
Related
I got this code right here, which works.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "Hello how are you";
char newString[10][10];
int i,j,ctr;
printf("\n\n Split string by space into words :\n");
printf("---------------------------------------\n");
j=0; ctr=0;
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[i]==' '||str1[i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else
{
newString[ctr][j]=str1[i];
j++;
}
}
printf("\n Strings or words after split by space are :\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
return 0;
}
but instead of char str1[100] I want to use an array of sentenceschar str1[2][100]
Meaning
char str1[2][100] = {"Hello how are you","I'm good, thanks"}
And these two sentences (or more) I want to be separated in separate words
char str1[100] = {"Hello","how","are","you"};
Actually, this is from a project for school, in which from a file, i have to store each sentence ended by '.'
If there is another way to store each sentece directly as words instead of sentences, it would be great help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LSIZ 128
#define RSIZ 10
void yodificacio(char* arr[], int index[], int n)
{
char* temp[n];
// arr[i] should be present at index[i] index
for (int i=0; i<n; i++){
temp[index[i]] = arr[i];
}
// Copy temp[] to arr[]
for (int i=0; i<n; i++)
{
arr[i] = temp[i];
index[i] = i;
}
}
int main()
{
int i = 0;
char *arr[] = {"Hey","there","how","are","you","all","today","idk"}; **Here I want the input to be char str1[2][100] = {"Hello how are you", "Im good thanks}, instead of char arr[] ...**
int index[] = {0,2,1,4,5,3,6,7};
int n = sizeof(arr)/sizeof(arr[0]);
yodificacio(arr, index, n);
printf("Reordered array is: \n");
for (int i=0; i<n; i++)
printf ("%s ", arr[i]);
return 0;
return 0;
}
Add an outer loop to process each sentence in the array.
int main()
{
char str1[2][100] = {"Hello how are you","I'm good, thanks"} ;
char newString[10][10];
int i,j,ctr;
printf("\n\n Split string by space into words :\n");
printf("---------------------------------------\n");
j=0; ctr=0;
for (int k = 0; k < sizeof(str1) / sizeof(str1[0]); k++) {
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[k][i]==' '|| str1[k][i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else
{
newString[ctr][j]=str1[k][i];
j++;
}
}
}
printf("\n Strings or words after split by space are :\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
return 0;
}
What's wrong with my code? I'm trying to count the numbers in a string after skipping all alphabets.
How to skip the alphabets and only count the numbers?
#include <stdio.h>
#include <stdlib.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if(a[i]>='a'&&a[i]<='z')
{
continue;
}
else
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[100001];
scanf("%s",a);
function(a);
printf("\n");
}
}
I switched your usage of scanf to use fgets. This works:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if (isdigit(a[i]))
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t = 0;
scanf("%d", &t);
while(t--)
{
char a[100001];
fgets(a, sizeof(a), stdin);
function(a);
puts("\n");
}
}
If the OP's intent was in fact to count up the groups of digits (in which the definition of 'number' is each contiguous run of digits), this will accomplish the goal:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int function(char a[])
{
size_t i = 0;
int cnt = 0;
// convert all non-digits to whitespace
printf("Converting\n");
for (char *p = a; *p != '\0'; ++p)
{
*p = isdigit(*p) ? *p : ' ';
}
printf("Found Groups: %s\n",a);
// note: strtok mutates the string it is given, it will not be usable after this:
for (char *gr = strtok(a," "); gr != NULL; gr = strtok(NULL, " "))
{
cnt++;
}
printf("Number of Groups: %d\n", cnt);
}
int main()
{
printf("Enter strings (Ctrl-C to end)\n");
while(1)
{
int result = 0;
char a[100001];
scanf("%s",a);
result = function(a);
printf("\n");
}
}
(I was not able to get the version using fgets to work, but how the string is gathered is not really in the scope of the question, the OP's original code in main is functional, I just made it an open-ended loop for my tests)
In your code continue statement is responsible for wrong answer. Because of it your code is not encountering the statement a[i] !='\0'. so suggest you to just avoid it and close your if statement without any code as like me. One more suggestion use also uppercase letters as user can also enter uppercase letters and using fgets() function you can also read white spaces.
Try out this
#include <stdio.h>
#include <stdlib.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if((a[i]>='a'&&a[i]<='z') || (a[i]>='A'&&a[i]<='Z'));
else
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[100001];
scanf("%s",a);
function(a);
printf("\n");
}
}
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){...}
I am passing a 2D char array to a function. If I print it in the calling function, I see it is filled with some elements. If I print it in the called function, only the first element is filled:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int compareTopString(char * string1, char * string2){
printf("%s %s\n", string1, string2);
return 5;
}
int getMaxTopics(int numP, int numTop, int * subject[][1000]){
for(int k = 0;k < numP;k++){
printf("%s\n", subject[k]);
}
return 0;
}
int main() {
int numP;
int numTop;
char subject[1000][1000];
scanf("%d", &numP);
scanf("%d", &numTop);
for (int i = 0; i < numP; i++){
scanf("%s", subject[i]);
}
printf("%d", getMaxTopics(numP, numTop, &subject));
for(int k = 0;k < numP;k++){
printf("%s\n", subject[k]);
}
return 0;
}
You are mixing char and int. Are you sure that compiled without warnings?
i'm trying to print a 2d array of string as practice(i'm a newbie) with no success i've tried every combination i could think of still nothing i'm sure i'm doing a silly error somewhere i just can't see it here some of the example:
using a pointer :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define lim 10
#define maxx 25
void print(char *);
int main()
{
int i = 1;
char input[lim][maxx];
char *ps = input;
printf("type the list of %d names or type quit to leave \n", lim);
while (i<lim && gets(input[i]) != NULL && strncmp(input[i], "quit", 4)!=0 ) {
i++;
}
printf("i've counted %d names\n", i);
print("\n");
print(ps);
return 0;
}
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a) != '\0') {
printf("%s\n", *(a+i));
i++;
}
}
here's the output:
type a list of %d names or type quit to leave :
bla
bli
blo
quit
i've counted 4 names
the list of names include :
segmentation fault (core duped)
another version of the print function is like this :
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
for(j = 0; j < maxx; j++){
puts(aray[i][j]);
//printf("%s\n", aray[i][j]);
}
}
}
i get the same output, can anyone help me debug this ? and thx in advance
In short, it looks like you need to brush up on your pointers. With your original print function:
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a) != '\0') {
printf("%s\n", *(a+i));
i++;
}
}
You are printing the value at a + i every iteration. This might sound like what you want, but what you actually pass to print is a pointer to an array of arrays of char (your compiler should be throwing a warning about incompatible pointer types). That is, the "proper" type of ps is (char *)[]. So in the print function you are only advancing the memory address by sizeof(char) with each iteration, whereas what you actually want is to increment it by sizeof(char) * maxx (the size of your array entries). To implement this change, do the following:
change declaration of print
void print(char (*)[maxx]);
change to proper pointer type
char (*ps)[maxx] = input;
And finally, change print function to something like:
void print(char (*a)[maxx]){
printf("the list of names include : \n");
int i;
for (i = 0; i < lim; i++){
printf("%s\n",*a);
a++;
}
}
You need not use the (a+i) syntax, as just advancing a by one each iteration accomplishes the same thing, and is possibly faster for large i. And of course, as others have mentioned, double check your new line printing, I believe you want printf('\n').
You are adding i as 1 which will not help in case of your two dimensional array as the next element will be at maxx location,so you can do something like this
//here lim and max are defined in your program
void print(char *a){
int i=0;
printf("the list of names include : \n");
while(i<(lim*maxx)){
printf("%s\n",a );
i += maxx;
a = a + maxx;
}
}
and the second variant should be
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
cout<<aray[i]<<"\n";
}
}
You start on index 1 in your 2d array, you should start with index 0
int i=1;
Your print function takes an array of characters and then does a printf string of each character which makes no sense
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a)!='\0')
{
printf("%s\n",*(a+i));
i++;
}
}
instead make it look like this
void print(char *a[], int strings)
{
int i = 0;
for (; i < strings; ++i)
{
puts( a[i] );
}
}
and call it with the number of strings you read
print(ps,i);
You would also be better off using fgets() instead of gets(), especially since your strings are max 25 chars so its easy to give a longer string. fgets() lets you specify the max size of the string fgets(input[i],maxx,stdin)
Your other function
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
for(j = 0; j < maxx; j++){
puts(aray[i][j]);
//printf("%s\n", aray[i][j]);
}
}
}
does a similar wrong assumption about the level of indirection
arra[i][j] is one character but puts takes a string argument, so puts( arra[i][j] ); is not correct, you could try fputc( arra[i][j], stdout ) instead since fputc takes one character
fix to
void print(char (*)[maxx]);
int main()
{
int i = 0;//int i = 1;
char input[lim][maxx] = { {'\0'}};
char (*ps)[maxx] = input;
printf("type the list of %d names or type quit to leave \n", lim);
while (i<lim && gets(input[i]) != NULL && strncmp(input[i], "quit", 4)!=0 ) {
i++;
}
printf("i've counted %d names\n", i);
printf("\n");//print("\n");
print(ps);
return 0;
}
void print(char (*a)[maxx])
{
int i=0;
printf("the list of names include : \n");
while(i<lim && a[i][0] != '\0') {
printf("%s\n", a[i]);
i++;
}
}