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;
}
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]
Im doing some work for Uni and wrote a programm which stores Integers in an char Array and converts them to their ASCII Values and prints them at the end. My code did not work before and only started working when i changed "%c" to "%i" in my scanf line. My question: Why does it have to be "%i" when i wanna store those Numbers in an char Array and not an Int Array. Thanks!
My code:
#include <stdio.h>
int main()
{
int i; /counter
char numbers[12];
printf("Please enter 12 Numbers\n");
for(i = 0; i < 12; i++){
printf("please enter the %i. Number\n", i+1);
scanf("%i", &numbers[i]);// <-- changed "%c" to "%i" and it worked.why?
}
for(i = 0; i < 12;i++){
printf("The %i.ASCII value is %i and has the Char %c\n", i+1, numbers[i], numbers[i]);
}
return 0;
}
%c is for reading a single character. So for example if you type in "123", scanf will read '1' into the char variable and leaves the rest in the buffer.
On the other side %i is the specifier for int and will therefore lead to undefined behavior when trying to read in a char.
I think what you are looking for is the %hhi specifier, which reads a number into a char variable.
I have written a code for searching a string among a group of strings but unlike we have to enter entire string before search, this code sorts out matching strings along with us entering each letter, like in Google search. But I don't know how to backspace string entry as it accepts string character by character.
C language only! Run the code once to understand better.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char string[10];
char s[5][10];
int i, j, k;
int e[5];
char g;
printf("\nEnter 5 Strings\n");
//Enter 5 to search from
for(i=0;i<5;i++)
{
printf("%d. ", i+1);
scanf("%s", &s[i]);
e[i]=1;
}
j=0;
do
{
system("cls");
printf("\nResults: ");
for(k=0;k<5;k++)
{
if(e[k]==1) //String displays if not striked by Input
printf("\n%s", s[k]);
}
printf("\n\nEnter Search String: ");
for(i=0;i<j;i++)
{
printf("%c", string[i]);
}
g = getche(); // Character Input
string[j]=g; // Character stored in string to compare
for(k=0;k<5;k++)
{
if(strncmp(string, s[k], j+1)!=0)
e[k]=0; //Used so if character doesn't match, string
eliminates
}
j++;
}while(g!='\r');
return 0;
}
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.
Basically I have a C program where the user inputs a number (eg. 4). What that is defining is the number of integers that will go into an array (maximum of 10). However I want the user to be able to input them as "1 5 2 6" (for example). I.e. as a white space delimited list.
So far:
#include<stdio.h>;
int main()
{
int no, *noArray[10];
printf("Enter no. of variables for array");
scanf("%d", &no);
printf("Enter the %d values of the array", no);
//this is where I want the scanf to be generated automatically. eg:
scanf("%d %d %d %d", noArray[0], noArray[1], noArray[2], noArray[3]);
return 0;
}
Not sure how I might do this?
Thanks
scanf automatically consumes any whitespace that comes before the format specifier/percentage sign (except in the case of %c, which consumes one character at a time, including whitespace). This means that a line like:
scanf("%d", &no);
actually reads and ignores all the whitespace before the integer you want to read. So you can easily read an arbitrary number of integers separated by whitespace using a for loop:
for(int i = 0; i < no; i++) {
scanf("%d", &noArray[i]);
}
Note that noArray should be an array of ints and you need to pass the address of each element to scanf, as mentioned above. Also you shouldn't have a semicolon after your #include statement. The compiler should give you a warning if not an error for that.
#include <stdio.h>
int main(int argc,char *argv[])
{
int no,noArray[10];
int i = 0;
scanf("%d",&no);
while(no > 10)
{
printf("The no must be smaller than 10,please input again\n");
scanf("%d",&no);
}
for(i = 0;i < no;i++)
{
scanf("%d",&noArray[i]);
}
return 0;
}
You can try it like this.