How to set input string array to accept only specific letters from STDIN??
char arr[testcases][100];
for(i=0;i<testcases;i++){
scanf("%99s",&arr[i]);
}
I am going to store only letters a,b,c,d in my array. How can I restrict other letters from getting stored in my array?
You can use this:
scanf("%99[a-d]", arr);
The return value will be 1 if any initial part of the string matches; input will stop at the first non-matching character.
Related
I am writing a function that takes in a user entered string consisting of o's g's and c's only. I would like to make an if statement that says if string contains any other character besides these three (reading string from left to right), return false, or something like that. How do I go through the string as an array to do this?
if (s[strspn(s, "ogc")] != 0) {
/* s contains a character other than o, g or c */
}
strspn(s, chars) returns the length of the longest prefix of s containing only characters in chars. If s contains only such characters, that prefix is all of s, so the character at that index will be the NUL terminator.
If your string might contain a newline or other whitespace after the desired characters, you'll need to adjust the test accordingly.
See man strspn for more information.
i have a given Input of four scanf-strings which i want to save in a multidimensional array. I Don't know if i save the string right, but i can't just simply print the whole array or certain characters of it.
char getr[4][4];
for (z=0; z<4; z++){
scanf(" %99s", &getr[z]);
}
for (s=0; s<4; s++) {
printf("%s\n",getr[s]);
}
Input:
abcd
efgh
ijkl
mnop
Output:
abcdefghijklmnop
efghijklmnop
ijklmnop
mnop
what if i just want to print the second line or the fourth character of the first line? Does anybody know?
First, change as following:
scanf(" %99s", getr[z]); //getr[z] is the address to take the 4 characters string
To print out the second line:
printf("%s\n", getr[1]);
To print out the fourth character of the first line:
printf("%c\n", getr[0][3]); // %c is used here because just print one character.
To store 4-char strings, like your example input, you need 5-char arrays to leave room for the terminating null character:
char getr[4][5];
Your scanf() format string should also reflect the amount of space you have; %99s could read up to 100 bytes (99 chars plus the null), and you only have 5 (including the null, after the above change). Also, &getr[z] and getr[z] happen to give the same address, but the types of the pointers are different -- but getr[z] gives a char * which is appropriate in this case. So...
scanf(" %4s", getr[z]);
Those changes will already keep the strings from running together, so individual strings can be accessed as getr[0] through getr[3]. To print individual characters within a string, add an additional set of indexing brackets and use a function that prints a character rather than a string:
fputc(getr[0][2],stdout); /* print 3rd char in 1st string */
printf("%c",getr[1][3]); /* print 4th char of 2nd string */
The reason you were getting all your strings concatenated was because the null char from the earlier strings overflowed into the next char array, and was overwritten by the first char of the next string you read. The last string's null would have overflowed past your whole 2D array into whatever was next in memory (which is bad).
I understand what is happening when I do this more explicitly using getchar(), putchar() and a while loop; however, I was just wondering is the storing and processing of an entered string the same when the code below is executed (behind the scenes)? Is each character being stored as one element of the array "typed"? How does scanf do this? etc.
#include <stdio.h>
int main(void)
{
char typed[500];
scanf("%[^\tEOF]", &typed);
printf("%s", typed);
return(0);
}
Thanks.
Yes, each character is being stored as one element of the array typed.
Note that your scan string looks for anything except a tab, an 'E', an 'O', or an 'F'; the 'EOF' in the pattern has no connection with EOF returned by getchar() et al on end of file. Also, your code is vulnerable to buffer overflows (hence the succinct comment from knittl) because you don't specify the size of the buffer in the format string. You'd be safer with:
scanf("%499[^\tEOF]", &typed);
Also, the return type of main() is int, not void.
Each element gets saved as elements of array. You should not use & when your variables are already arrays. An array name is already an address.
getchar() has the default delimiter: return key and takes input as character only
while scanf allows various types of input by specifying the format: %s, %d etc
You can also specify the delimiter for scanf (default are space, tab, enter)
In your case, the delimiter is specified as tab or EOF.
How can I extract numbers from a char array, separated with spaces, convert them to integers and sum them? For example:
"34 54 3 23"
I'd start at the beginning of the array, check each character in turn with isdigit() and keep a current value and a current total.
When reaching the terminating NUL char (or last element of the array), the current total is already calculated.
You need to parse the string.
If you know how many integers are in there, you could use just a sscanf.
Otherwise find out where blanks are (with something similar to strtok, for example) and then read integers using atoi
writing another program, it reads a txt file, and stores all the letter characters and spaces (as \0) in a char array, and ignores everything else. this part works.
now what i need it to do is read a user inputted string, and search for that string in the array, then print the word every time it appears. im terrible at I/O in C, how do you read a string then find it in a char array?
#include <stdio.h>
...
char str [80];
printf ("Enter your word: ");
scanf ("%s",str);
char* pch=strstr(fileData,str);
while (pch!=NULL)
{
printf ("found at %d\n",pch-fileData+1);
pch=strstr(pch+1,str);
}
read in the user inputted string as a char array as well (cause strings are basically char* anyway in C)
use a string matching algorithm like Boyer-Moore or Knutt-Morris-Pratt (more popularly known as KMP) - google for it if you like for C implementations of these - cause they're neat, tried and tested ways of searching strings for substrings and pattern matches and all.
for each of these indexOf cases, print the position where the word is found maybe? or if you prefer, the number of occurrences.
Generally, the list of C string functions, found here, say, are of the format str* or strn*, depending on requirements.
One for-loop inside another for-loop (called nested loop). Go through all the letters in your array, and for each letter go through all the letters in your input string and find out if that part of the array matches with the input string. If it does, print it.