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;
}```
Related
#include <stdio.h>
#include <string.h>
int countLetters(char *string1, char letter){
int count=0;
for(int i=0; i<strlen(string1); i++){
if(string1[i]=letter){
count++;
}
}
return count;
}
int main(){
char string1[200];
char letter;
int count;
printf("\nEnter the string: \n");
fgets(string1, 200, stdin);
printf("\nEnter character to be searched: \n");
scanf("%c", &letter);
count = countLetters(string1, letter);
printf("\nThe number of occurrences: %d", count);
}
I was expecting for the function to output the number of times each letter of the array was equal to the char(letter) inputted by the user, but it is just giving me the length of the string.
Change the line:
if(string1[i]=letter){
to
if(string1[i]==letter){
Note, that the string1[i]=letter was overwriting data in string1[i].
You have to use equal equal to operator instead of assignment operator in if condition like this
if(string1==latter)
in your if condition if(string1=latter) value of latter variable is assign to string1[i]
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 !
I know that I can use
scanf("%d %d %d",&a,&b,&c):
But what if the user first determines how many input there'd be in the line?
You are reading the number of inputs and then repeatedly (in a loop) read each input, eg:
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int numInputs;
int *input;
printf("Total number of inputs: ");
scanf("%d", &numInputs);
input = malloc(numInputs * sizeof(int));
for (int i=0; i < numInputs; i++)
{
printf("Input #%d: ", i+1);
scanf("%d", &input[i]);
}
// Do Stuff, for example print them:
for (int i=0; i < numInputs; i++)
{
printf("Input #%d = %d\n", i+1, input[i]);
}
free(input);
}
Read in the whole line, then use a loop to parse out what you need.
To get you started:
1) Here is the manual page for getline(3):
http://man7.org/linux/man-pages/man3/getline.3.html
2) Some alternatves to getline:
How to read a line from the console in C?
3) Consider compressing spaces:
How do I replace multiple spaces with a single space?
4) Use a loop for parsing. You might consider tokenizing:
Tokenizing strings in C
5) Be careful and remember that your user could enter anything.
#include <conio.h>
#include <stdio.h>
main()
{
int a[100],i,n_input,inputs;
printf("Enter the number of inputs");
scanf("%d",&n_input);
for(i=0;i<n_input;i++)
{
printf("Input #%d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n_input;i++)
{
printf("\nInput #%d: %d ",i+1,a[i]);
}
}
/*
_______________This program is in C Programming Language_______________
We have to directly enter all the elements in one line giving spaces between them. Compiler will automatically ends the for loop I have used and assign the value to their respective variables or array indexes. Below program and output will give you better understanding.
*/
#include <stdio.h>
int main()
{
//taking no of inputs from user
int len;
printf("Enter the number of inputs you want to enter : ");
scanf("%d", &len);
int i;
//defined an array for storing multiple outputs
int arr[100];
//included a printf statement for better understanding of end user
printf("Enter the inputs here by giving space after each input : ");
/*here is the important lines of codess for taking multiple inputs on one line*/
for (i=0;i<len;i++)
{
scanf("%d", &arr[i]);
}
printf("Your entered elements is : ");
for (i=0;i<len;i++)
{
printf("%d ", arr[i]);
}
}
/*
OUTPUT :
Enter the number of inputs you want to enter : 5
5 5 5 8 7
Your entered elements is : 5 5 5 8 7
*/
I want to print three char variables with a for loop but I don't know why only last is printed.
This is my code:
int main (){
int i, j, k;
char word[50], old[1], new[1];
printf("Enter a word: ");
gets(word);
printf("Enter desired letter to substitute: ");
gets(old);
printf("Enter the new letter: ");
gets (new);
for (i = 0; i<strlen(word); i++){
printf("%c", word[i]);
}
for (j = 0; j<strlen(old); j++){
printf("%c", old[j]);
}
for (k = 0; k<strlen(new); k++){
printf("%c", new[k]);
}
}
I don't get why this simply code doesn't work.
Your code overflows the buffers for old and new because they do not contain enough space for a 1 character string. In C strings needs to be null terminated, so a char array must always be one element longer than the maximum number of characters it must contain. For example your word array can only hold a 49 character string, since the 50th element must be the null.
So old and new must both be char[2] arrays.
Your loops are unnecessary and incorrect. You can print the values directly without specifying the index. In your post you're trying to loop through each index and print a character at a time. Here's what you should do:
int main (){
int i, j, k;
char word[50], old[2], new[2];
printf("Enter a word: ");
gets(word);
printf("Enter desired letter to substitute: ");
gets(old);
printf("Enter the new letter: ");
gets (new);
printf("%s\n", word);
printf("%s\n", old);
printf("%s\n", new);
}
Help me to get out of this problem. I'm using GCC on ubuntu12.04. While I write this program to get 5 strings from keyboard n then print these strings on screen. Program is compiled but during execution it takes strings from keyboard but print only last string. The program which I have written is below:
void main()
{
char names[10];
int i,j;
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%s",names);
}
for(i=0;i<5;i++)
printf(" the names you enter are %s\n", names);
}
1) you can use 2D char array in this way
char names[5][100];
each line in the 2D array is an array of char with size = 100
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%99s",names[i]);
}
2) You can use array of pointers in this way
char *names[5];
each element in the array is a pointer to a string (char array). you have to assign each pointer in the array to a memory space before you call scanf()
for(i=0;i<5;i++)
{
names[i]=malloc(100);
printf(" Enter a name which you want to register\n");
scanf("%99s",names[i]);
}
3) if you compile with gcc version >2.7 then your scanf() can allocate memory by using "%ms" instead of "%s"
char *names[5];
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%ms",&names[i]);
}
There is a simple example about reading and keeping string in the char array.
#include <stdio.h>
const int MACRO = 6;
int main() {
printf("Hello Admin Please Enter the Items:\n");
char items[MACRO][20];
for (int i = 0; i < MACRO; ++i) {
scanf("%19s", items[i]);
}
for (int i = 0; i < MACRO; ++i) {
printf("%s ", items[i]);
}
return 0;
}
In your program the mistake is that you have not putted '&'address of operator int the first for loop . names in your case is an array if you store %s string in names and not &names[0] or &names[1] or so on then as array itself acts as a pointer therefore the array "names" is pointing to the address of its first elements i.e. names[0] . so if you are writing scanf("%s",names); that is similar to scanf("%s",&names[0]); so as you are storing the names in one element only and that too for 5 iterations for only the last string you have entered will be stored and previous strings will be gone . so onlye last string is printed in your program .
in your code, you only declare char data type to be one dimensional and thus it will always overwrite the previous input,that's why the result is the last input printed 5 times.
char names[10];
the above declaration means that you declare a char type variable only with 10 character size without an extra array,it means you only declare a single variable for 5 input.
to make a two dimensional char, you will need to declare it like this :
char names[5][10];
in the code above, it means that you declare a char type variable with 10 character size in an array of 5.
Here is the code I wrote using pointer.
#include <stdio.h>
void main()
{
char *string[100];
int ln;
printf("Enter numbar of lines: ");
scanf("%d",&ln);
printf("\n");
for(int x=0;x<ln;x++)
{
printf("Enter line no - %d ",(x+1));
scanf("%ms",&string[x]); // I am using gcc to compile file, that's why using %ms to allocate memory.
}
printf("\n\n");
for(int x=0;x<ln;x++)
{
printf("Line No %d - %s \n",(x+1),string[x]);
}
}
Another code using two dimensional Array
#include <stdio.h>
void main()
{
int ln;
printf("Enter numbar of lines: ");
scanf("%d",&ln);
printf("\n");
char string[ln][10];
for(int x=0;x<ln;x++){
printf("Enter line no - %d ",(x+1));
scanf("%s",&string[x][0]);
}
for(int x=0;x<ln;x++)
{
printf("Line No %d - %s \n",(x+1),string[x]);
}
}