Creating string array with recursion - arrays

I should take strings from user WORDSIZE times.
And I should do this with recursion.
In this code any loop, any static variables, and string library function not allowed.
So I send count and n variable as a pointer.
#include <stdio.h>
#define WORDSIZE 5
/* I accepted wordsize as number of the words to be delete. To delete "hello"and"world", wordsize should be 2 */
int take_words(char (*array)[WORDSIZE], int *m, int *cnt)
{
if((*m) == 0)
return 0;
else
{
scanf("%s", array[*cnt]);
getchar();
printf("(1)This word will be entered %s\n", array[*cnt]);
//This printf works correctly.
(*m)--;
(*cnt)++;
take_words(array, m, cnt);
}
}
int main()
{
char (*words)[WORDSIZE];
int n=WORDSIZE, count=0;
printf("Enter the words one by one\n");
take_words(words, &n, &count);
for(int i=0; i<WORDSIZE; i++)
printf("(2)Occured array's %ith term: %s\n",i, words[i]);
}
Problem is that, althoug printf(1) works correct, printf(2) in main doesn't give the correct result. Here is the output:
Enter the words one by one
hello
(1)This word will be entered hello
darkness
(1)This word will be entered darkness
my
(1)This word will be entered my
old
(1)This word will be entered old
friend
(1)This word will be entered friend
(2)Occured array's 0th term: hellodarknmy
(2)Occured array's 1st term: darknmy
(2)Occured array's 2nd term: my
(2)Occured array's 3rd term: old
(2)Occured array's 4th term: friend
Array couldn't create right. How could I hanlde this?

Related

Words frequency with number of letters (basic loop only)

I Want to write C program to print words length and their frequency by letters number with basic loops techniques. I could get the word length work but I stuck with frequency
(example: Do 2 not 3 judge 5 a 1 book 4 (had solved this))
there are # words with 1 letter
there are # words with 2 letter
etc...
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
printf("Please enter a word: ");
for (i = 0; i < 30 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
printf("%s %d ", word, b);
b = 0;
}
return 0;
}
Your question wasn't completely clear. But from what I understood, you also wanted to print the number of times(frequency) a word of length 'l' is inputted by the user. So I will answer that :
You could just store the length of the word in an array that the user inputs. Once all the inputs are read, you can just print the frequency of each word length from the stored array
Refer the following code to understand what I meant :
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
int word_length_freq[30]={0}; //an array which will store the frequency of word length(all initialized to 0)
//eg. if word is "hello" it will increase count of word_length_freq[5] by 1
printf("Please enter a word: ");
for (i = 0; i < 3 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
word_length_freq[b]++;
printf("%s %d ", word, b);
b = 0;
}
for(int i=1;i<30;i++){ //This will print the frequency of all words from length 1 to 30
printf("There are %d words of length %d\n",word_length_freq[i],i);
}
return 0;
}
I hope this solves your question !

How to pass an array made of strings in C

I'm just trying to write a simple code where I enter a 3 letter word and then print out the word I entered. I tried doing this by creating the array, and then making a loop where the counter "i" keeps incrementing and the scan function keeps working for each index value of the letter I add.
But there seems to be some error in line 16, and even then not sure if the logic of the code is right.
#include <string.h>
#define ALEN 3
int main (void)
{
char array[ALEN];
int i;
printf("Enter a 3 letter word> ");
scanf("%s", array);
for(i=0; i<ALEN; i++)
{
array[i] = array[ALEN];
scanf("%s", &array[i]);
}
printf("\n");
printf("Word entered: %s", char array[i]);
return 0;
}```

Sort words in a string based on their vowel number

I must write a program in C that allows an user to say how many words they want to enter in a string and then I must sort those words based on their vowel number(the word with more vowels is first and the one with the least vowels is last - if two words have the same number of vowels then leave them in the order as they have appeared). For example:
string - "Aaaa Bbbbbbb B CD home Aaa BB A poke"
Sorted string - "Aaaa Aaa home poke A Bbbbbbb B CD BB"
I know how to write the first part, but for the sort part I have no idea. Can someone help me with that please?
EDIT: Currently I have this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning (disable: 4996)
int main(){
char string1[20], string2[100] = { '\0' };
int N, i;
do{
printf("Enter the number of words you want to enter in the string: ");
scanf("%d", &N);
if (N < 2){
printf("You must enter at least two words.\n");
printf("Enter the number of words you want to enter in the string: ");
scanf("%d", &N);
}
} while (N < 2);
for (i = 0; i < N; i++){
printf("Enter a word: ");
scanf(" %[^\n]", string1);
if (strlen(string2) == 0)
strcpy(string2, string1);
else {
strcat(string2, " ");
strcat(string2, string1);
}
}
printf("%s\n", string2);
return 0;
}
You will want to create an array of structs that hold a pointer to the word and then the number of vowels in each word. Something like:
struct vowelcnt {
char *word;
int numvowels;
}
Then sort the array of structs (descending order based on numvowels. Then simply loop through the sorted structs outputting the word which would give you the words sorted in order of the number of vowels contained. Hope that helps.

how to display entered letter in c?

I am new to c programming. I have created a program for entered letters and finally displayed the entered letters.. but it displayed only final letters always.. please help .. i know its simple question but am beginner so please help guys..
#include<stdio.h>
int main()
{
char z;
int a;
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%s",&z);
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%s\n",&z);
}
return 0;
}
Problems:
You should use %c (for character) instead of %s (for string).
Use a character array for storing multiple characters. Read about arrays here.
Remove & from printf() in the second for loop.
Try this:
int main()
{
char z[10]; //can hold 10 characters like z[0],z[1],z[2],..
int a;
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",&z[i]);
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%c\n",z[i]);
}
return 0;
}
Please look into this for more details on scanf. You have given scanf("%s",&z); %s is for reading strings(array of chars except newline char and ended with null char). So if you put this inside loop you wont get desired result. And if you want read only a char at a time use %c here c for Character.
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",z+i);
}
char z is a place holder for one character only. And you are over writing what you set z to in the for loop. To take in more characters, use a char array as others have mentioned.
Or print the characters in the same you loop you are scanning them:
#include<stdio.h>
int main()
{
char z;
int a;
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%s",&z);
printf("letter scanned:%c\n", z);
}
return 0;
}
Letters are scanned using %c. And to scan multiple letters you can use char array: char z[10];
What you are trying to do can be done this way:
char z[10]; // Take some max size array
...
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",&z[i]); // Scan the letters on each array position.
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%c\n",z[i]); //'printf' doesn't require address of arg as argument hence no `&` required
}
%s argument is used to scan a string of chars.
Note the difference between string of chars and array of chars. The string of chars in C needs to be terminated with ASCII Character 0 represented as \0 in char format, while the array of char is just a collection of letters which need not be terminated with \0.
The difference becomes more important when you try to perform some operation on strings such as printf, strcpy, strlen, etc.. These functions work on null character termination property of string.
For Example: strlen counts the characters in the string till it finds \0, to find out the length of string. Similarly, printf prints the string character by character until it finds the \0 character.
UPDATE:
Forgot to mention that scanf is not a good option to input char format. Use fgetc instead, with stdin as input FILE stream.
#include <stdio.h>
int main()
{
char *z;
int a;
printf("enter the no.");
scanf("%d",&a);
z = (char *) malloc(a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%c",z+i);
}
printf("the entered letters are:");
for(i=0;i<a;i++)
{
printf("%c\n",z);
}
return 0;
}
first error in your code is you have used "%s" instead of "%c".Second is it is impossible to store multiple values in one variable so instead of using variable use arrays.third is that you have told the user to enter the number of character that he/she wants to entered which you don't know.They can enter 1 also and 100000 also so the number of members in array is not defined.Better is to use specific number of characters in array.
finally i got the answer thank you for the help stackoverflow guys simply rocks ...
#include<stdio.h>
#include<malloc.h>
int main()
{
int a;
char *z=(char *)malloc(sizeof(a));
printf("enter the no.");
scanf("%d",&a);
printf("the entered no. is:%d\n",a);
int i;
for(i=0;i<a;i++)
{
printf("enter the letters:");
scanf("%s",&z[i]);
}
printf("the entered letters are:\n");
for(i=0;i<a;i++)
{
printf("%c\n",z[i]);
}
return 0;
}

String Compare issues in C

So the assignment is this: Problem C: Practice Strings (lastnames.c)(8 points)
Read in n, then n lastnames, and check to see if the first in the list is ever repeated again.
Sample Run #1
Enter n, followed by n Last names (each last name must be a single word):
5 Reagan Bush Clinton Bush Obama
First name in list is not repeated.
Sample Run #2
Enter n, followed by n Last names (each last name must be a single word):
4 Bush Clinton Bush Obama
First name in list is repeated.
I can get the first two names to compare, but I can't figure out how to compare the first to whatever is in the second string array. I don't want to post my code in the event that someone searches this and copies mine. I'll send it to you though. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// initializing character strings
char last[25], first[25];
// initializing number of names, and the index for the number of names
int index, n;
// read in the number
printf("Enter n, followed by n Last names (each last name must be a single word) :\n");
scanf("%d", &n);
scanf("%s", first);
for (index = 0; index < n; index++)
scanf("%s", last);
if (strcmp(last, first)== 0)
{
printf("First name in list is repeated.\n");
}
else
{
printf("First name in list is not repeated.\n");
}
return 0;
}
In your forloop the lastvariable is overwritten every time which means that you are only comparing the first input to the last name in the next sequence of n lastnames.
If you want to count the number of repetitions, use a counter within the for-loop.
int nRepetitions = 0;
/* ... read the numbers and the first string
(therefore index should start with 1)... */
for (index = 1; index < n; index++) {
scanf("%s", last);
if (strcmp(last, first) == 0) {
nRepetitions++;
}
}
#include <stdio.h>
#include <string.h>
int main(void){
char last[25], first[25];
int index, n, repeated = 0;
printf("Enter n, followed by n Last names (each last name must be a single word) :\n");
scanf("%d", &n);
scanf("%s", first);
for(index = 1; index < n; index++){ //index = 1 : aleady input first
scanf("%s", last);
if(strcmp(last, first)== 0)
repeated = 1;
}
if(repeated)
printf("First name in list is repeated.\n");
else
printf("First name in list is not repeated.\n");
return 0;
}
Your code is incorrect..
For loop last has just 1D character array and which is overwritten everytime.
So Use a 2D array instead..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
// initializing character strings
char first[25];
// initializing number of names, and the index for the number of names
int index=0, n;
// read in the number
printf("Enter the number of last names :\n");
scanf("%d", &n);
printf("Enter the first name \n");
scanf("%s", first);
char last[n][25];
for (int i = 0; i < n; i++)
scanf("%s", last[i]);
for(int i=0;i<n;i++)
{
if (strcmp(last[i], first)== 0)
index++;
}
if(index==0)
printf("First name not repeated\n");
else
printf("First name repeated %d times", index);
return 0;
}

Resources