Return 2d char array in C - c

I want to take a 2D char array as an input in a function (actually my 2D array is global and the function doesn't have inputs) , change the values in it and then return another 2D char array.
char stoixeia[50][7];
int main(int argc, char *argv[])
.
.
.
if (strcmp(answer, "Gift")==0)
{
gift();
}
char gift ()
{
int i,j,m;
int wrong=0;
int k=0;
char usern[50];
while(wrong=0)
{
printf("Enter the username that you want to Gift:\n");
scanf("%s", &usern);
for (i=0; i<50; i++)
{
if (*usern==stoixeia[i][0])
{
wrong=1;
k=i;
}
}
}
m=strlen(usern);
for(i=0; i<m; i++)
{
stoixeia[k][6]= stoixeia[k][6] + 10;
}
return stoixeia[50][7];
}
My thought was that if i declare my array as a global one everything would change in my functions and the array will get "updated". The compiler doesn't show any errors but when I run the programm and my answer is Gift the .exe stops working. Can you suggest me anything? Thank you

your function must be like this:
You don't need to return a value because you change directly the global variable.
void gift ()
{
int i,j,m;
int wrong=0;
int k=0;
char usern[50];
while(wrong==0) /* replace = by ==*/
{
printf("Enter the username that you want to Gift:\n");
scanf("%s", usern);
for (i=0; i<50; i++)
{
if (usern[i]==stoixeia[i][0])
{
wrong=1;
k=i;
}
}
}
m=strlen(usern);
for(i=0; i<m; i++)
{
stoixeia[k][6]= stoixeia[k][6] + 10;
}
}

As already mentioned use **gift() see here for some more information

Related

how to initialize a vector of unknown size the value 0

Im doing this exercise that requires to read a string and print out the length of each word.
Thing is supposedly we arent supposed to know how many words this sting has.
In this case for example its 4
viVa la VIDA loca
This is the part of the code i have made.
int textStats(char* filename);
int main()
{
FILE*file;
file=fopen("file","r");
if(file==NULL)
{
printf("Error in opening the file ");
exit(1);
}
textStats(file);
}
int textStats(char* filename)
{
int i,j;
int n=0;
int N=20;
char str[N];
while(fscanf(filename,"%c",&str[n])!=EOF)
{
n++;
}
for(i=0; i<n; i++)
{
printf("%c",str[i]);
}
i=j=0;
int count[]={0};
while(str[i]!='\0')
{
if(isalpha(tolower(str[i])))
{
count[j]++;
}
if(isspace(str[i])|| str[i]=='\0')
{
j++;
}
i++;
}
for(i=0;i<j;i++)
{
printf("word %d with count %d\n",i+1,count[i]);
}
}
What i dont understand why is it that if i initialize count[]={0} it will print errors but if i initialize it to count[]={0,0,0,0} it will return a correct value?
Firstly, you don't need to store each word's length in an array. You can just print them out whenever you finish reading 1 word
{
FILE*file;
file=fopen("file","r");
if(file==NULL)
{
printf("Error in opening the file ");
exit(1);
}
textStats(file);
}
int textStats(char* filename)
{
int i,j;
int n=0;
int N=20;
char str[N];
while(fscanf(filename,"%c",&str[n])!=EOF)
{
n++;
}
for(i=0; i<n; i++)
{
printf("%c",str[i]);
}
i=j=0;
int count=0;
while(str[i]!='\0')
{
if(isalpha(tolower(str[i])))
{
count++;
}
if(isspace(str[i])|| str[i]=='\0')
{
j++;
printf("word %d with count %d\n",j,count);
count = 0;
}
i++;
}
}
If you really want to store each word's length inside an array, you can explore the use of dynamically growing array
int count[]; is invalid, it's attempting to declare an array of incomplete size. int count[] = {0} creates an array matching the initializer list, in this case an array of 1 int items.

How can I write a function which returns array with repeating strings grouped together?

This is the origional question:
Write a function repeating_group(char* arr[], int n);
where,
arr[]: predefined array of string to operate on
n: size of the arr[]
and returns char* res[] with repeating strings grouped together.
Example:
input:
char* arr[] = {"tut", "slf", "tut", "lzyy", "slf", "tut"};
output:
char* res[] = {"tut", "tut", "tut", "slf", "slf", "lzyy"}
This is my code:
#include<stdio.h>
char repeating_group(char*arr[],int n)
{
int i,j;
printf("char*res[]= {");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
printf("\"%s\",\"%s\",",arr[i],arr[j]);
}
}
}
printf("}");
}
int main()
{
char*arr[]={"Carrot","Tomato","Mustard","Carrot","Mustard","Tomato","Potato","Brinjal"};
int n=sizeof(arr)/sizeof(arr[0]);
repeating_group(arr,n);
getchar();
return 0;
}
The problem here is that it only prints the stuff that is needed but do not return an array in which the elements should be stored.
As i am new to C too, i edited your code and it worked but as i don't know shorting and other method i did it by my way there will be many other and easy way to do it rather then mine. some element which are not the same where getting left and not been stored so to solve that is have to create new array there are two new array arr1 and arr2 in the program
#include<stdio.h>
char repeating_group(char*arr[],int n)
{
int i,j,k=0;
char *arr1[n];
char *arr2[n];
for(i=0;i<n;i++)
arr2[i]=arr[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr2[j]==0)
continue;
if(arr[i]==arr[j])
{
arr1[k++]=arr2[i];
arr1[k++]=arr2[j];
arr2[i]=0;
arr2[j]=0;
}
}
}
for(i=0;i<n;i++)
if(arr2[i]!=0)
arr1[k++]=arr2[i];
printf("char* arr[]= {");
for(i=0;i<n;i++)
printf("\"%s\",",arr[i]);
printf("}");
printf("\n\n");
printf("char* res[]= {");
for(i=0;i<n;i++)
printf("\"%s\",",arr1[i]);
printf("}");
return 0;
}
int main(void)
{
char *arr[]={"Carrot","Tomato","Mustard","Carrot","Mustard","Tomato","Potato","Brinjal"};
int n=sizeof(arr)/sizeof(arr[0]);
repeating_group(arr,n);
//getchar();
}
it works!!

How can I store String value in char array in C?

I have char array to store string values. I wanted to store the value of a string variable into the char array.
char Password[30];
char User[2];
int i;
for(i=0; i<5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password,30);
}
I wanted to input the values for the array and it should throw a buffer overflow but I couldn't do it. How can I do it?
This should work for you:
#include <stdio.h>
#include <string.h>
int main() {
char Password[30];
char User[5][30];
int i;
for(i = 0; i < 5; i++) {
printf("Enter Password");
scanf("%s", Password);
strcpy(User[i],Password);
}
for(i = 0; i < 5; i++)
printf("Password %d: %s\n", i+1, User[i]);
return 0;
}
The second for loop is to show the output and that every thing is stored right!

printing a 2d array of string in c

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

sprintf(): scaning only the first value in float array

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

Resources