how to split a string using space delimiter in C unix? [duplicate] - c

This question already has answers here:
C function for separate a string in array of chars
(5 answers)
Closed 5 years ago.
I am new in C programming, my training uses Redhat, Unix and I already spent my day searching for a solution. I know manipulating a string in C is difficult for me as beginner.
How can I split the string into individual words so I can loop through them?. Or convert a string into char array to be able to access individual elements.
char myString[] = "the quick brown fox";
To be exact i want to print each word of the said string into a fixed column and whenever the the string reached that number of column it will go to new line and print the sequence without splitting the word.
eg. print it within 12 columns only w/out splitting the word:
the quick
brown fox
and not:
the quick br
own fox
..TIA

Your problem can be split into two parts, part A you need to split the sentence into words and part B you need to print a maximum of x characters per line.
Part A - Split the String
Take a look at the strok function. You can use space as the delimiter.
#include <stdio.h>
#include <string.h>
// You need this line if you use Visual Studio
#pragma warning(disable : 4996)
int main()
{
char myString[] = "the quick brown fox";
char* newString;
newString= strtok(myString, " ,.-");
while (newString!= NULL)
{
printf("%s\n", newString);
newString= strtok(NULL, " ,.-");
}
return 0;
}
Output:
the
quick
brown
fox
Part B
Now you want to print the words and insert a newline when you reach the max columns 12 in your example.
You need to check the length of every extracted word, you can use strlen for that.
When the string is to long you insert a newline...
Hope it helped, if something is unclear leave a comment.

Related

How to extract string-only from stdin using Scanf?

I am a beginner of C and trying to extract Characters from standard input.
Input = "C0h1r2i3s4"
Expected Outcome = "Chris"
I've tried two ways to achieve this:
Use scanf to store input in one variable -> traverse through input one character a time -> if that character is not a number in ASCII table, store that character in a variable
Use fgets to get input and store in one variable -> traverse through input one character a time -> if that character is not a number in ASCII table, store character in a variable
I wonder if it's possible to use scanf/fgets to get only the characters from stdin? So that I don't have to traverse through every characters.
I've tried to use scanset below, but it seems scanf always screens at character-level and stops when the next char does not fit specified format.
Anyway, I wonder if there is a more powerful use of scanset & scanf.
Code for scanf()
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[50];
//intput = C0h1r2i3s4
scanf("%s", &str); // str = "C0h1r2i3s4"
//intput = C0h1r2i3s4
scanf("%*c%*d%s", &str); // str = "h1r2i3s4" -> C & 0 is ignored
//intput = C0h1r2i3s4
scanf("%[A-Z,a-z]%*d%s", &str); // str = "C" -> are they a valid format identifier? "%[A-Z,a-z]%*d%s"
}
Just want to close this question in case someone else is looking for similar one
In C, there is no simple way to extract certain characters directly from stdin.
first, we need to read the complete inputs from stadin
then, we travers through the inputs to decides which characters are in concern
Thank you, the-busybee, for your comment that saved my life earlier this year. I cannot add a comment due to not enough reputation on GitHub, and I just realized that I could reply to my own post.

Starting word of the sentence not printed using c programming language

when I am trying to concatenate a word and a sentence the first word of the sentence automatically deleted, what improvement should I use.. my code is: enter code here`
char word[]="AddMe";
char adder[150];
// reading string from stdin
scanf("%[^\n]*c",%sentence);
strcat(adder,word);
strcat(adder,sentence);
puts(adder);
First, use:
strcpy(adder,word);

Separating a single string into two different strings in C [duplicate]

This question already has answers here:
Split string with delimiters in C
(25 answers)
Closed 9 years ago.
I take user input in char name[20] using fgets like this:
fgets(name,20,stdin);
The user enters two strings separated by white space like John Smith. What if I wanted to use John and Smith in two strings like char name[20] , char surname[20] or just compare John and Smith using strcmp?
I tried a lot, but I did not find any way to do this.
What are some ways to fix this kind of problem?
You need to learn char * strtok (char *restrict newstring, const char *restrict delimiters) function in C uses to splitting a string up into token separated by set of delimiters.
You input string John Smith is separated by space (' ') char. You need to write a code something like below:
char *token;
token = strtok(name, " "); // first name
strcpy(fname, token);
token = strtok(NULL, " "); // second name
strcpy(lname, token);
You will need to search for the blank within the string yourself - look up the strchr function for that. Then, use strncpy to copy the two parts in 2 different strings.
Use the strtok function to split strings.

C: Printing a separate letter of a string to the screen [duplicate]

This question already has answers here:
Output single character in C
(5 answers)
Closed 8 years ago.
Hi I'm new to C and I'm having trouble finding a way of printing only the 4th, 5th or 10th letter of a String.
I've got this little code:
char firstWord[100];
char secondWord[100];
printf("Please type in: Hello World\n");
fgets(firstWord, sizeof(firstWord), stdin);
printf("Please type in: How are you?\n");
fgets(secondWord, sizeof(secondWord), stdin);
printf("You typed: %s,%s", firstWord, secondWord);
strcat(firstWord, secondWord);
printf("together it looks like this: %s", firstWord);
Now how would I print for instance the 4th or the 6th character only of the concatenated string?
A string in C is just an array of chars (with a '\0' at the end), so you can access the individual characters with an array-subscript:
printf("%c", firstWord[3]); // don't forget 4th element is at [3]
// & use %c for char
Try this
printf("%c", firstword[3]); // for 4th character
Since you're using array style char strings, you can just:
printf("%c", firstWord[3]);
For the fourth letter, and on like that. The reason it's 3 is because the array indexing starts at zero, so the first term is in the zero element, second term in the first element, etc. Then you just continue that way with all of them.
It's a lot easier if you have a pattern, though, because then you can just code a loop on the pattern, and it won't take as much effort on the coder's part.

in C: reading input string, finding it in char array

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.

Resources