Want to store "char values" in array (in C language) - c

I want to store "char data type values" in an array, but it doesn't work.
First, I tried using "gets"
but it gave me a run time error.
Code was like this
int tmp = 0;
char arr[100] = { 0, };
while (arr[tmp]!=NULL)
{
gets(arr[tmp]);
tmp++;
}
for (int rtmp = 0; rtmp < a; rtmp++)
printf("%s ", arr[rtmp]);
return 0;
In a second way, I was using "scanf", but I couldn't store "char data type(it should be more than one character like string)", but only one character was available.(I tried %s, but it doesn't work)
Plus, it doesn't print the last value of array.
int a = 0;
scanf("%d", &a); //determine how much I input values
int tmp = 0;
char arr[100] ={ 0 , };
for(tmp=0;tmp<a;tmp++)
{
scanf("%c ",arr[tmp]);
fflush(stdin);
}
for (int rtmp = 0; rtmp < a; rtmp++)
printf("%c ", arr[rtmp]);
return 0;
The most "identical" for me is
without notifying "a" values("a" means how much values I input)
and storing "char values" in array..
How can I solve this problem?
Thanks in advance! Your help is always appreciated :)

Array of char data type is called Sting. You can take a string's input using scanf() and gets(). But if you use scanf(), the string will be input by pressing Space-bar or Enter. But if you use gets(), input will be given only by pressing the Enter key.
Example 1:
char s[100];
scanf("%s", s);
Example 2:
char s[100];
gets(s);
Now, if you want to input every single character individually, you can do that also:
char s[100], c;
int n, i, j;
scanf("%d", &n);
getchar();
for(i=0; i<n; i++) {
scanf("%c", &s[i]);
}
s[i] = '\0';
Now look, I wrote a getchar() after scanf("%d", &n);, because when you press enter after inputting n, a new line character ('\n') is also taken as input in the character next to n. So you must do this in case like this.
One more thing, you can take input any string containing spaces using scanf() also. Just do this:
char s[100];
scanf("%[^\n]", s);

You can not enter the individual character by following snippet of code.
char arr[10] = {0};
//unsigned char ch;
unsigned int i = 0;
printf("Enter the array elements\n");
for(i = 0; i<10; i++)
{
scanf("%c", &arr[i]);
printf("i = %d and arr[%d] = %c\n", i, i, arr[i]);
}
The result of this snippet is as per this image. enter image description here
To avoid this issue, it is recommended to enter the character without new line charactoer '\n' or without pressing enter.
Or other method is to use the array as string and input the character by gets() like this snippet.
char s[100];
gets(s);

Related

printing characters using array in c language?

I tried to scan and print the characters of array using below code but input characters are not matching with output characters
#include <stdio.h>
int main() {
char s[10];
int i, n;
printf("enter the value of n:\n");
scanf("%d", &n);
printf("start entering the characters:\n");
for (i = 0; i < n; i++) {
scanf("%c", &s[i]);
}
for (i = 0; i < n; i++) {
printf("%c", s[i]);
}
return 0;
}
OUTPUT
enter the value of n:
5
start entering the characters:
ABCDE(scanf values)
ABCD(printf values)
Can anyone please clarify my doubt why is the output not matching with input
Since you are wanting to read data into a character array with "scanf" you probably could just reference the string identifier instead and simplify things. Following are a few tweaks to your code that still inputs the data and prints it back out.
#include <stdio.h>
#include <string.h>
int main()
{
char s[10];
int i, n;
printf("enter the value of n:\n");
scanf("%d", &n);
printf("start entering the characters:\n");
scanf("%s", s); /* In lieu of using a loop */
if (strlen(s) < n) /* Just in case less characters are entered than was noted */
n = strlen(s);
for (i = 0; i < n; i++)
{
printf("%c", s[i]);
}
printf("\n");
return 0;
}
The program just scans in the complete string instead of a character at a time. Also, I included the "<string.h> file so as to use functions such as "strlen" (get the length of the string) to provide a bit more robustness to the code. Running the program netted the same character set that was entered.
:~/C_Programs/Console/InputOutput/bin/Release$ ./InputOutput
enter the value of n:
7
start entering the characters:
ABCDEFG
ABCDEFG
You might give that a try.
Regards.

Program that receives size of string and lets you create it letter by letter in C

Ive got this program which im stuck with in which I am trying to receive the size of a string and following it, telling the user to input the letter by letter. Here is my code.
#include<stdio.h>
#include<stdlib.h>
int main(){
int size;
int i;
char letter;
printf("Your string's size is...?: ");
scanf("%d", &size);
char mystring[size] ;
for (i=0; i<size; i++){
printf("Write a letter: \n");
scanf("%c", &letter);
mystring[i] = letter;
}
printf("%s",mystring);
system("PAUSE");
return 0;
}
Thanks!
Two problems here.
First, the %c format specifier to scanf will read any character, including a newline. You need to put a space before it to absorb any newlines in the input buffer:
scanf(" %c", &letter);
Second, you don't null-terminate the string, nor do you leave enough space in the array to store the null terminator. Make the array one element larger, and add the null byte at the end:
char mystring[size+1];
for (i=0; i<size; i++){
...
}
mystring[size] = 0;
Two things:
First, you need to terminate the string with a \0-character. Otherwise, printf will result in undefined behaviour.
Second, note that scanf("%c",..) will probably consume a new line left in the buffer when a user presses "enter" after having entered a number (i.e. the size).
Write:
char mystring[size+1] ;
for (i=0; i<size; i++){
printf("Write a letter: \n");
scanf("%c", &letter);
if (i==0 && letter == '\n') {
i--;
continue;
}
mystring[i] = letter;
}
mystring[size] = '\0';
printf("%s",mystring);

How can I get a string from user without spaces using scanf?

This is a C code to get a string for types of brackets '()' & '<>' & '{}' & '[]' from user. The length of this string is n and it is an user input.
int main()
{
long int n;
int i;
scanf("%lld", &n);
char array[n];
for(i=0; i<n ; i++)
{
scanf("%s", &array[i]);
}
}
The problem is I want to get the string without any spaces between them from user. But, this code is working for the input with space between each character and give the correct result.
For example, if I type {(() the program won't run. but if I type { ( ( ) the program shows the correct result.
How can I solve this problem?
Change:
scanf("%s", &array[i]);
to this:
scanf(" %c", &array[i]);
since what you attempt to do is read your string character by character.
Please notice the space before %c, which will consume the trailing newline that is going to be left in the stdin buffer from when you entered n.
I had wrote about the caution when reading a character with scanf() here.
Now, even if you use {(() or { ( ( ) for the input, it will be the same, since scanf() will ignore the whitespaces.
However, you should null terminate your string, if you want it to be used by standard functions, which you almost certainly want. For example, if you were to use printf("%s", array);, then you must have array null terminated.
An approach to this, assuming that the user will input correctly (in a perfect world), you could do that:
#include <stdio.h>
int main()
{
long int n;
int i;
scanf("%ld", &n);
// create an extra cell to store the null terminating character
char array[n + 1];
// read the 'n' characters of the user
for(i=0; i<n ; i++)
{
scanf(" %c", &array[i]);
}
// null terminate the string
array[n] = '\0';
// now all standard functions can be used by your string
printf("%s\n", array);
return 0;
}
PS: scanf("%lld", &n); --> scanf("%ld", &n);. Use your compiler's warnings! It will tell you about it..
If you want to make sure the user types at least 1 space character between each 'valid' character, you can just wait in the loop until the user adds a space character.
char c;
for (i = 0; i &lt n; i++)
{
c = '\0';
while (c != ' ') // wait for the user to type a space character
{
scanf ("%s", &c);
}
while (c == ' ') // wait for the user to type something else
{
scanf ("%s", &c);
}
array[i] = c;
}

Substring in C (what will happen to the remaining in array)

I have given a task to generate a sub string for the given input in C. The code as follows.
#include<stdio.h>
int main(){
char a[1000];
char *sub;
int startFrom = 0;
int endAt = 0;
printf("Enter the String: ");
scanf("%s", a);
printf("Start From? ");
scanf("%d", &startFrom);
printf("End At? ");
scanf("%d", &endAt);
sub = &a[startFrom];
a[endAt] = '\0';
printf("%s\n", sub);
return 0;
}
The code however works fine, but what will happen to the rest of the characters in the array?
The rest of the array remains the same; it's just that you changed one of the characters in the array to null('\0'). So if you try to access any other character after (or before) the a[endAt] character, you would be able to do so.
Check it out Your code with some extra at Ideone.com.
However as you can see, when you try to print the original array, it would be printed only till the first '\0' character.

String Editor (not functioning correctly)

I'm new to C and have been set the following problem. I am to write a program where a string can be entered and stored, I should then enter two integer values which will then be used to remove characters from the string, afterwards the result should be printed. Once the program works it should be converted into a function.
I have created a program that will split the entered string into two strings which store the chars I want to keep in two buffers, afterwards the two strings are concatenated to give the resultant edited string. The problem I am having is that when I print the edited string I get random characters at the end and sometimes in between the two strings and I think it's because the strings are not being null terminated correctly. I hope that someone is able to help, Thanks :)
#include <stdio.h>
#include <string.h>
int main ()
{
char string [25];
char buffer1 [25];
char buffer2 [25];
int start;
int remove;
int i;
int finish;
int size;
int numbercopy;
int A, B, C;
printf("Enter a string: ");
gets(string);
printf("\nEnter a starting character position: ");
scanf("%d", &start);
printf("\nHow many characters would you like to remove? ");
scanf("%d", &remove);
finish = (start+remove);
size = strlen(string);
numbercopy = (size-finish);
strncpy(&buffer1[0], &string[0], start);
buffer1[start] = '\0';
strncpy(&buffer2[0], &string[finish], numbercopy);
buffer2[numbercopy] = '\0';
A = strlen(buffer1);
B = strlen(buffer2);
C = (A+B);
strcat(buffer1, buffer2);buffer1[C] = '\0';
for (i=0; i<25; i++)
{
printf("%c", buffer1[i]);
}
return 0;
}
Since it is a string, you do not need to print it character by character. Also, the loop indicates that only 25 char strings will be printed. If a string (buffer1) is shorter in length(<25), garbage values will be printed, if a string is is larger (>25), some chars will not be printed.
Change this:
for (i=0; i<25; i++)
{
printf("%c", buffer1[i]);
}
to this:
printf("%s", buffer1);

Resources