I am trying to do this programming project in the book i study:
Write a program that sorts a series of words entered by the user:
Enter word: foo
Enter word: bar
Enter word: baz
Enter word: quux
Enter word:
In sorted order: bar baz foo quux
Assume that each word is no more than 20 characters long. Stop reading when the user enters an empty word (i.e., presses Enter without entering a word). Store each word in a dynamically sllocated string, using an array of pointers to keep track of the strings.
After all words have been read, sort the array (using any sorting technique) and then use a loop to print the words in sorted order.
That is what i am trying:
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 20
int compare ( const void * p, const void * q);
int main (void){
int n;
printf("How many words do you want to write");
scanf("%d", &n);
fflush(stdin);
char * word[n];
for( int i = 0; i < n; i++){
fprintf(stdout , "Waiting for the word:");
if(fgets(word[i] , MAX_LENGTH +1 , stdin) == "\n")
break;
}
qsort((void *)word,n,sizeof(int),compare);
printf("Ordered list is:\n\n");
for( int i = 0; i < n; i++){
fprintf(stdout , "\t %s", word[i]);
}
}
int compare (const void * p, const void * q){
return strcmp( * (char**) p , * (char **) q);
}
Edit : question with italic is solved thanks to fellow coders here. My new issue is i can't read past the first element of array. And program closes itself.
C:\Users\Lenovo\Desktop\ogrenme\ch17>sorting
How many words do you want to write4
Waiting for the word:asd
C:\Users\Lenovo\Desktop\ogrenme\ch17>
And there is one single annoying error that keeps me prevented from completing the exercise and "chill" with rest of this challenging book:
sorting.c:20:5: warning: implicit declaration of function 'qsort' [-Wimplicit-function-declaration]
qsort((void *)word,n,sizeof(int),compare);
I did a small trial (definitely the code could be optimized)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
int n = 0;
char **string_list;
char *tmp;
//allocate memory for the first element
string_list = (char**)malloc(sizeof(char*)*1);
//infinite loop
while ( 1 ) {
//allocate size of each element to a max of 20 chars
string_list[n] = (char*)malloc(sizeof(char)*20);
printf("Enter word : ");
fgets (string_list[n], 20, stdin);
//remove trailing return carriage
string_list[n][strlen(string_list[n]) - 1] = '\0';
//break the loop here if user enters empty string
if (strlen(string_list[n]) < 1) {
break;
}
//add counter
n++;
//add memory to contain another element
string_list = realloc(string_list, sizeof(char*)*(n+1));
}
printf("\n\nInitial List is:\n");
for (int i=0 ; i<n-1 ; i++) {
printf("%s - ", string_list[i]);
}
printf("%s\n\n", string_list[n-1]);
//sorting the list
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (strcmp(string_list[i], string_list[j]) < 0) {
tmp = string_list[i];
string_list[i] = string_list[j];
string_list[j] = tmp;
}
}
}
printf("Sorted List is:\n");
for (int i=0 ; i<n-1 ; i++) {
printf("%s - ", string_list[i], strlen(string_list[i]));
}
printf("%s\n\n", string_list[n-1], strlen(string_list[n-1]));
}
output
$ ./sort
Enter word : foo
Enter word : bar
Enter word : baz
Enter word : quux
Enter word :
Initial List is:
foo - bar - baz - quux
Sorted List is:
bar - baz - foo - quux
Related
Problem
I have made sorting program which is similiar to other found at
https://beginnersbook.com/2015/02/c-program-to-sort-set-of-strings-in-alphabetical-order/
but program which i made is not working.
I think both are same but my program giving me waste output.
Also i want to know in other program count is set to 5 for example and it should take 6 input starting from 0 but it is getting only 5,How?
My Program
#include <string.h>
#include <stdio.h>
int main() {
char str[4][10],temp[10];
int i,j;
printf("Enter strings one by one : \n");
for(i=0;i<5;i++)
scanf("%s",str[i]);
for(i=0;i<5;i++)
for(j=i+1;j<5;j++)
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
printf("\nSorted List : ");
for(i=0;i<5;i++)
printf("\n%s",str[i]);
printf("\n\n");
return 0;
}
Use qsort().
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int pstrcmp( const void* a, const void* b )
{
return strcmp( *(const char**)a, *(const char**)b );
}
int main()
{
const char* xs[] =
{
"Korra",
"Zhu Li",
"Asami",
"Mako",
"Bolin",
"Tenzin",
"Varrick",
};
const size_t N = sizeof(xs) / sizeof(xs[0]);
puts( "(unsorted)" );
for (int n = 0; n < N; n++)
puts( xs[ n ] );
// Do the thing!
qsort( xs, N, sizeof(xs[0]), pstrcmp );
puts( "\n(sorted)" );
for (int n = 0; n < N; n++)
puts( xs[ n ] );
}
Please don’t use bubble sort. In C, you really do not have to write your own sorting algorithm outside of specialized needs.
Here is a program which will sort and print your inputted strings. Answering a little late, but just in case others have a similar question.
// This program will sort strings into either ascending or descending order
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
#define EQUAL 0
#define ASCENDING 0
#define DESCENDING 1
// Function prototypes
void swap_str(char str1[], char str2[]);
void sort_strings(char str[MAX_SIZE][MAX_SIZE], int len, int order_type);
void print_2d_array(char str[MAX_SIZE][MAX_SIZE], int len);
int main(void) {
int order_type;
char str[MAX_SIZE][MAX_SIZE];
// User selecting the order type
printf("-----------[Order Type]-----------\n");
printf("Sort your strings in ascending or descending order?\n");
printf("0 = Ascending | 1 = descending\n");
scanf("%d", &order_type);
if (order_type != ASCENDING && order_type != DESCENDING) {
printf("Please enter 0 or 1\n");
exit(1);
}
// User inputting their strings
printf("---------[Enter Strings]----------\n");
printf("Enter Strings one by one.\n");
printf("Max Strings: %d | Max String Len: %d\n", MAX_SIZE, MAX_SIZE);
int i = 0;
while ((i < MAX_SIZE) && (scanf("%s", str[i]) == 1)) i++;
if (i == MAX_SIZE) printf("You reached the maximum strings allowed\n");
// Program output of the sorted strings
printf("---------[Sorted Strings]---------\n");
sort_strings(str, i, ASCENDING);
print_2d_array(str, i);
return 0;
}
// Swaps two strings (assuming memory allocation is already correct)
void swap_str(char str1[], char str2[]) {
char temp[MAX_SIZE];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
// Will sort a 2D array in either descending or ascending order,
// depending on the flag you give it
void sort_strings(char str[MAX_SIZE][MAX_SIZE], int len, int order_type) {
int i = 0;
while (i < len) {
int j = 0;
while (j < len) {
if ((order_type == ASCENDING) &&
(strcmp(str[i], str[j]) < EQUAL)) {
swap_str(str[i], str[j]);
} else if ((order_type == DESCENDING) &&
(strcmp(str[i], str[j]) > EQUAL)) {
swap_str(str[i], str[j]);
}
j++;
}
i++;
}
}
// Will print out all the strings 2d array
void print_2d_array(char str[MAX_SIZE][MAX_SIZE], int len) {
int i = 0;
while (i < len) {
printf("%s\n", str[i]);
i++;
}
}
Example (Ascending order):
-----------[Order Type]-----------
Sort your strings in ascending or descending order?
0 = Ascending | 1 = descending
0
---------[Enter Strings]----------
Enter Strings one by one.
Max Strings: 1000 | Max String Len: 1000
Mango
Watermelon
Apple
Banana
Orange
// I pressed CTRL+D here (Linux) or CTRL+Z then enter (on Windows). Essentially triggering EOF. If you typed the MAX_SIZE it would automatically stop.
---------[Sorted Strings]---------
Apple
Banana
Mango
Orange
Watermelon
it should take 6 input starting from 0 but it is getting only 5,How?
This loop
for(i=0;i<5;i++)
scanf("%s",str[i]);
execute for i being 0, 1, 2, 3, 4 so it loops 5 times.
If you want 6 loops do
for(i=0;i<=5;i++)
^
Notice
or
for(i=0;i<6;i++)
^
Notice
Also notice this line
char str[6][10],temp[10];
^
Notice
so that you reserve memory for 6 strings
This is not an answer, but some criticism of the code you refer to:
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
puts("How many strings u are going to enter?: ");
scanf("%d",&count); // (1)
puts("Enter Strings one by one: ");
for(i=0;i<=count;i++) // (2)
gets(str[i]);
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("Order of Sorted Strings:"); // (3)
for(i=0;i<=count;i++)
puts(str[i]);
return 0;
}
And the criticism:
(1) scanf("%d",&count); reads a number into count, and returns after that. It does not consume the line break(!)
(2) this loop does not print anything, just reads. However if you put
for(i=0;i<=count;i++){
printf("%d:",i);
gets(str[i]);
}
in its place, you will suddenly see that it asks for names 0...5, just skips the 0 automatically. That is where the line break is consumed, it reads an empty string. You can also make it appear, if instead of putting 5 into the initial question, you put 5 anmoloo7.
(3) in the printout the names appear below the title Order of Sorted Strings. But there is no linebreak in that printf. The thing is that the empty string is "smaller" than any other string, so it gets to the front of the list, and that is printed there first. If you do the 'trick' of appending a name after the initial number, the output will look different, there will be 6 names, and one of them appended directly to the title.
Plus there is the thing what you probably get from your compiler too: gets is a deadly function, forget its existence and use fgets with stdin as appears in other answers.
I have this sample that I made:
#include <stdio.h>
#include <string.h>
void main()
{
char str[100],ch;
int i,j,l;
printf("\n\nSort a string array in ascending order :\n");
printf("--------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
/* sorting process */
for(i=1;i<l;i++)
for(j=0;j<l-i;j++)
if(str[j]>str[j+1])
{
ch=str[j];
str[j] = str[j+1];
str[j+1]=ch;
}
printf("After sorting the string appears like : \n");
printf("%s\n\n",str);
}
#include <stdio.h>
#include <string.h>
#define STRING_MAX_WIDTH 255
void sort_strings(char str_arr[][STRING_MAX_WIDTH],int len){
char temp[STRING_MAX_WIDTH];
for(int i=0;i<len-1;i++){
if(strcmp(&str_arr[i][0],&str_arr[i+1][0])>0){
strcpy(temp, str_arr[i+1]);
strcpy(str_arr[i+1],str_arr[i]);
strcpy(str_arr[i],temp);
sort_strings(str_arr, len);
}
}
}
int main(){
char str_arr[][STRING_MAX_WIDTH] = {"Test", "Fine", "Verb", "Ven", "Zoo Keeper", "Annie"};
int len = sizeof(str_arr)/STRING_MAX_WIDTH;
sort_strings(str_arr, len);
for(int i=0;i<len;i++){
printf("%s\r\n", str_arr[i]);
}
}
i have a string that is made out of a few sentences.
for example:
hello world bye bye
now, i need to make this sentence into a coulmn of words:
hello
world
bye
bye
i have this idea going on, but i dont know how to write it correctly, so i was hopiny ypu guys could help me out.
this is what i have so far:
int len=0, k=0, stopatspace=0;
char temptext[100][15]={0};
char line[300]={0};
len=strlen(line);
printf("len is: %d", len);
for(k=0; k<len; k++)
{
if (k == ' ')
{
// i dont know what to write here in order to make it a cloumn
}
}
basiclly, my idea is to run on all the length of my line and when i reach a space i want it to enter (to go one line down so that it will look like a coulmn)
Suppose line is the char array that contains hello world bye bye and text is declared as
char text[100][15]; //I used 100 and 15 because your example contains it
and you want each word to be copied into each row of text. Then,use strtok() function with " "(space) as delimeter and place this in a loop that terminates when strtok() returns NULL to get each word. Copy each word to each row of text using strcpy() in the loop.
The code for this will look like this:
char text[100][15];
char line[]="hello world bye bye";
int i=0;
char *token=strtok(line," ");
while(token!=NULL)
{
strcpy(text[i],token);
i++;
token=strtok(NULL," ");
}
Now, to print it,you can use
for(int j=0;j<i;j++)
printf("text[%d]=%s",j,text[j]);
Another method would be to manually copy each character until a space is seen.
int len=strlen(line);
int i=0;
int k=0;
for(int j=0;j<len+1;j++)
{
if(line[j]==' ')
{
text[i][k]='\0';
i++;
k=0;
}
else
{
text[i][k]=line[j];
k++;
}
}
Note that the above code does not prevent buffer overflows. You can print each word using
for(int j=0;j<i+1;j++)
printf("text[%d]=%s",j,text[j]);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char string[100];
fgets(string, 100, stdin);
string[strlen(string)-1] = 0;
// create an array of pointers
char **string_array = (char**)malloc(sizeof(char*));
int i = 0, array_size;
// tokenize input
char *token = strtok(string, " ");
while(token!=NULL) {
// dynamically increase the array during run time
string_array = (char**)realloc(string_array, (i+1)*sizeof(char**));
// create the string as you would do when there is only one string
string_array[i] = (char*)malloc(strlen(token)+1);
strcpy(string_array[i], token);
token = strtok(NULL, " ");
i++;
}
array_size = i;
for(i=0; i<array_size; i++) {
printf("%s\n", string_array[i]);
}
return 0;
}
Basically you create an array of pointers and you allot memory for the strings one by one as you would do when there is only one string.
(if number of token is unknown, use realloc to increase the size of pointer to pointers.)
#include <stdio.h>
#include <ctype.h>
int main(void){
char line[300] = "hello world bye bye\n";
char temptext[100][15]={0};
int i=0, j, k=0;
while(line[k]){
if(isspace(line[k])){
++k;//skip space to word
continue;
}
for(j=0; j < 15-1 && line[k] && !isspace(line[k]); ++k, ++j)
temptext[i][j] = line[k];
if(j && ++i == 100)
break;
}
for(j=0; j<i; ++j)
puts(temptext[j]);
return 0;
}
#include<stdio.h>
#define NEWLINE printf("\n")
int main(void)
{
char string[]="hello world bye bye";
int index=0;
while(string[index])
{
if(string[index]==32)
{
NEWLINE;
index++;
}
else
{
printf("%c",string[index]);
index++;
}
}
NEWLINE;
}
// Whenever i encountered with a space, i am printing a new line on the screen. Here 32 is the ASCII value for space
I am trying to do something as simple as printing the reverse of a string .
EXAMPLE :
Hello World! This is me
Needed O/P:
me is This World! Hello
My code goes something like this:
#include<stdio.h>
#include<string.h>
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%s",&arr[i]);
for(i=0;i<size;i++)
{
printf("%s\n",&arr[size-1-i]); //overwritten words
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is %s\n",(char *)revarr);
}
I except arr[0] , arr[1] etc to be separate entities but on printing and storing them they seem to be overlapping like this :
i/p:
Hello World
o/p:
World
HellWorld
the reversed sentence is WorlHell##$
I can't seem to figure out what is wrong!
Thanks in advance!
EDIT :
On printing
printf(&arr[0]);
printf(&arr[1]);
I get :
HellWorld
World
What I expected it to print is
Hello
World
You declared arr and revarr as array of char pointers. You need to dynamically allocate memory for their elements.
Also note that you do not need & in statements
scanf("%s",&arr[i]);
and
printf("%s\n", &arr[size-1-i]);
// ^No need of &
Here is the modified version of your code. Note that there is no need to use revarr to reverse the string.
#include<stdio.h>
#include<stdlib.h>
int main(){
size_t i, size;
printf("Enter the number of words\n");
scanf("%d", &size);
char *arr[size] ; // Variable length array. Supported by C99 and latter
for(i = 0; i < size; i++)
{
arr[i] = malloc(20); // Assumimg words are no longer than 20 characters
scanf("%s", arr[i]);
}
printf("The reversed sentence is:\n");
for(i = size-1; i >= 0; i--) // Run loop in reverse order and print words
printf("%s ", arr[i]);
}
You haven't allocated memory for arr[0], arr[1], etc. before using them to read strings in
scanf("%s",&arr[i]);
That is cause for undefined behavior. You need something like:
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
{
// Allocate memory.
// make it large enough to hold the input
arr[i] = malloc(100);
scanf("%s", arr[i]);
}
for(i=0;i<size;i++)
{
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is: ");
for(i=0;i<size;i++)
{
printf("%s ", revarr[i]);
}
printf("\n");
// Deallocate the memory.
for(i=0;i<size;i++)
{
free(arr[i]);
}
return 0;
}
This is a good approach for your problem :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
/* the string line will contain the line of the input */
char line[100];
/* read the string with the function f gets */
fgets(line,100,stdin);
/* the tab will contain all the string of the variable line */
char *tab[20];
/* the variable p will point to each string of the line */
char *p=NULL;
/* we extract the strings of the line via the function strtok */
p=strtok(line," ");
int nb=-1;
while (p!=NULL)
{
nb++;
/* we allocate a space memory fo every str ing */
tab[nb]=malloc(sizeof(char)*100);
strcpy(tab[nb],p);
p=strtok(NULL," ");
}
/* there is an exception with the last string of the line we need to take care o f it */
tab[nb][strlen(tab[nb])-1]='\0';
int i;
/* print the strings in reverse or der */
for (i=nb;i>=0;i--)
{
printf("%s ",tab[i]);
/* dont forget to free the space memory at the end of the prog ram */
free(tab[i]);
}
printf("\n");
return 0;
}
You need the following
#include <stdio.h>
#include <string.h>
int main(void)
{
size_t size;
printf( "enter the number of words: " );
scanf( "%zu", &size );
char arr[size][20];
char revarr[size][20];
for ( size_t i = 0; i < size; i++ ) scanf( "%s", arr[i] );
printf( "\n" );
for ( size_t i = 0; i < size; i++ ) strcpy( revarr[i], arr[size-i-1] );
printf( "the reversed sentence is" );
for ( size_t i = 0; i < size; i++ ) printf( " %s", revarr[i] );
printf( "\n" );
return 0;
}
If to enter
2
Hello World
then output will be
World Hello
Take into account that the code will be compiled only if your compiler supports C99. Otherwise you have to allocate memory dynamically for character arrays.
As for your code then it has undefined behaviour and in whole is invalid. You did not allocate memory for each element of arrays arr and revarr. You may not assign one array to another. Instead you have to use standard function strcpy and so on.
I need to build an Coin Dispenser application. I've written the withdrawal and deposit parts but in item inventory creation I couldn't create a solution to that. Item names should be taken from keyboard as strings. Item prices corresponding to the items should be taken from keyboard as unsigned ints.
Arrays should be variable-sized, aka VLAs. I've googled it and found some resources about creating VLAs, I think I should allocate memory to them dynamically using malloc. I couldn't do that, so I basically created limited sized arrays with using BUFSIZ.
How can I use VLAs in this case, and how can I fill them with strings?
Also I should prevent buffer overrun. This will be the second case.
Macros and code blocks:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMBER_OF_COINS 5
#define ITEM_NAME_LEN 9
...
void editorMain(void)
{
printf("Please enter the number of items: ");
scanf("%u", &itemQuantity);
printf("\n\n");
char ** itemNames[BUFSIZ] = {0};
unsigned int itemPrices[BUFSIZ] = {0};
printf("Please enter the names of items: \n");
for (int i = 0; i < itemQuantity; ++i) {
printf("#%d: ", i + 1);
gets(** itemNames[i]);
puts("");
}
printf("Please enter the prices of items: \n");
for (int i = 0; i < itemQuantity; ++i) {
printf("#%d: ", i + 1);
scanf("%u", &itemPrices[i]);
puts("");
}
dispenserMain(*itemNames, itemPrices);
return;
}
Also program should output the item names and prices as a list. Function dispenserMain does this:
void dispenserMain(char * itemNames[], unsigned int itemPrices[]) {
...
for (counterItemNames = 1; counterItemNames <= itemQuantity; ++counterItemNames) { //the for loop will print out the item name and prices with their correspondence.
printf("%10u.%14s%12.2f TL\n", counterItemNames, itemNames[counterItemNames - 1], currencyConverter(itemPrices[counterItemNames - 1]));
}
...
}
Edit (new code):
void editorMain(void)
{
printf("Please enter the number of items: ");
scanf("%u", &itemQuantity);
printf("\n\n");
char itemNames[itemQuantity][ITEM_NAME_LEN+1];
memset(&itemNames, 0, sizeof(itemNames));
printf("Please enter the names of items: \n");
char line[ITEM_NAME_LEN+1];
for (int i = 0; i < itemQuantity; ++i) {
printf("#%d: ", i + 1);
if (! fgets(line, sizeof(line), stdin) || !line[0])
// abort...
// fgets leaves the trailing newline in the input
if (line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = 0;
}
snprintf(itemNames[i], ITEM_NAME_LEN+1, "%s", line);
puts("");
}
unsigned int itemPrices[] = {0};
printf("Please enter the prices of items: \n");
for (int i = 0; i < itemQuantity; ++i) {
printf("#%d: ", i + 1);
scanf("%u", &itemPrices[i]);
puts("");
}
dispenserMain(itemNames, itemPrices);
return;
} //end globalMenuOut
It outputs:
Welcome to CoinedDispenser!
Author: Buğra Ekuklu
Contact: bugraekuklu [at] hotmail.de
1. Dispenser
2. Editing variables
3. Readme
Please make a choice: 2
Please enter the number of items: 3
Please enter the names of items:
#1:
#2: blabla
#3: glagla
Please enter the prices of items:
#1: 45
#2: 65
#3: 75
................... AVAILABLE ITEMS oo
# Item Name Price
= ========= ======
1.
0.45 TL
2. blabla
0.65 TL
3. glagla
0.75 TL
Enter your item selection:
Dispenser function:
void dispenserMain(char (*itemNames)[ITEM_NAME_LEN+1], unsigned int itemPrices[])
{
printf("%s\n\n", " ................... AVAILABLE ITEMS oo");
printf("%10s%15s %14s\n", "#", "Item Name", "Price");
printf("%10s%15s %14s\n", "=", "=========", "======");
puts("");
unsigned int counterItemNames = 0; //initializing counter
for (counterItemNames = 1; counterItemNames <= itemQuantity; ++counterItemNames) { //the for loop will be print out the item name and prices with their correspondence.
printf("%10u.%12s%12.2f TL\n", counterItemNames, itemNames[counterItemNames - 1], currencyConverter(itemPrices[counterItemNames - 1]));
}
puts("");
...
}
Where you write char ** itemNames[BUFSIZ] = {0}; should be:
char itemNames[itemQuantity][ITEM_NAME_LEN+1];
This is a VLA because it has an array that has a dimension known at
runtime. Space allocated via malloc is not a VLA.
Unfortunately VLAs cannot have initializers, so you would also have
to write:
memset(&itemNames, 0, sizeof itemNames);
If you want to dynamically allocate this array it would be:
char (*itemNames)[ITEM_NAME_LEN+1];
itemNames = calloc( itemQuantity, sizeof *itemNames );
and in both cases the usage will be identical. itemNames[i] designates an array which you have allocated.
Your code gets(** itemNames[i]); has two problems; it dereferences a null pointer, and it uses the gets function which allows the user to cause a buffer overflow. You should be reading into the array itemNames[i], which after using my suggestion above, designates storage you've allocated.
Unfortunately it's awkward in C to just input a string of fixed length. You have to worry about what happens if the user types more than your line length. If that doesn't concern you too much then:
char line[BUFSIZ];
if ( ! fgets(line, sizeof line, stdin) || !line[0] )
// abort...
// fgets leaves the trailing newline in the input
if ( line[strlen(line)-1] == '\n' )
line[strlen(line)-1] = 0;
snprintf(itemNames[i], ITEM_NAME_LEN+1, "%s", line);
To call your function do:
dispenserMain(itemNames, itemPrices);
and the function has to be
void dispenserMain(char (*itemNames)[ITEM_NAME_LEN+1], unsigned int itemPrices[]);
If you want to use your original the syntax for dispenserMain then you have to allocate an array of pointers, as opposed to using a 2-D array as I have described. In that case you would use char **itemNames = malloc(itemQuantity * sizeof *itemNames); and the loop through and malloc each pointer to point to some more storage of length ITEM_NAME_LEN+1.
Objective:-To print A3B5 as shown below..
AAABBBBB
I have achieved this through following code:-
#include <stdio.h>
#include <conio.h>
void main(){
char char1[2],*ptr_char,c;
int number[2],*ptr_number,i,j=0,k=0;
ptr_char = char1;
ptr_number = number;
printf("Enter character string\n");
for(i=0;i<2;i++)
scanf("%c",&char1[i]);
printf("Enter number array\n");
for(i=0;i<2;i++)
scanf("%d",&number[i]);
for(i=0;i<ptr_number[j];i++)
printf("%c",ptr_char[k]);
j++;
k++;
for(i=0;i<ptr_number[j];i++)
printf("%c",ptr_char[k]);
getch();
}
///////////////////////////////////////////////////////////
Now i have to take a single array in which my A3B5 can be taken instead of taking two array.
But i am unable to take array like that.
I want to take A3B5 in single array .
Is this possible?
You can say:
char csrc[]= "A3B5" ;
The issue here is that 3 and 5 are not numbers, they are characters. So you will have to convert from "3" to 3. You have several options for that, including sscanf and atoi. As others have suggested, read in the entire string at once, and it will be in the char array, and then decompose it into the separate parts you want.
check this code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int count = 0, i = 0;
char temp = 0;
char ip[16] = {0};
char filter[8] = {0};
int num[8] = {0};
printf("Enter input :\t");
scanf("%s", ip);
for (count = 0, i =0; count < strlen(ip); count++, i++)
{
filter[i] = ip[count]; //take out the character to print
count++;
temp = ip[count]; //read the number of print iteration as a character
num[i] = atoi(&temp); // convert "n" [charater] to n [int]
}
for (count = 0; count < strlen(ip)/2 ; count++)
{ //print loop
for (i = 0; i < num[count]; i++)
printf("%c\t", filter[count]);
printf("\n\n");
}
return 0;
}
Sample run and o/p
[sourav#localhost test]$ ./a.out
Enter input : S2G5
S S
G G G G G
[sourav#localhost test]$