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);
Related
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.
I have started learning c. Currently, I am trying to get words out of two char arrays so that I can compare them using my helper method called compare. However, my strtok() is giving me weird output.
Heres my code:
char *headerPointer=headers;
char *linePointer=firstline;
printf("Header-%s\n",headers);
printf("Line-%s\n",firstline);
headerPointer=strtok(headerPointer,",");
linePointer=strtok(linePointer,",");
while ((headerPointer!=NULL&&linePointer!=NULL)) {
printf("\nPrinting words from headers\n");
printf("%s",headerPointer);
headerPointer=strtok(NULL,",");
printf("\nPrinting words from line\n");
printf("%s",linePointer);
linePointer=strtok(NULL, ",");
}
Output produced by above code:
Header-Hello,My,name,is,Ram.
Line-I,own,20,thousand,bucks.
Printing words from headers-
Hello.
Printing words from line-
I.
Printing words from headers-
own.
Printing words from line-
20.
Printing words from headers-
thousand.
Printing words from line-
bucks.
I don't understand why header is printing the contents from line and where does my,name,is,ram go?
I tried to write them using the following code and this same code produced the desired output.
Different Code Style:
char *headerPointer=headers;
char *linePointer=firstline;
printf("Header%s\n",headers);
printf("Line%s\n",firstline);
headerPointer=strtok(headerPointer,",");
while(headerPointer!=NULL)
{
printf("\nPrinting words from headers\n");
printf("%s",headerPointer);
headerPointer=strtok(NULL,",");
}
linePointer=strtok(linePointer,",");
while(linePointer!=NULL){
printf("\nPrinting words from line\n");
printf("%s",linePointer);
linePointer=strtok(NULL, ",");
}
Output:
Header-Hello,My,name,is,Ram.
Line-I,own,20,thousand,bucks.
Printing words from headers-
Hello.
Printing words from headers-
My.
Printing words from headers-
name.
Printing words from headers-
is.
Printing words from headers-
Ram.
Printing words from line-
I.
Printing words from line-
own.
Printing words from line-
20.
Printing words from line-
thousand.
Printing words from line-
bucks.
Please explain why two codes based on a same idea are producing different results? Can we modify the first code to give results like second? I tried to search and followed the solution already available but couldn't get far.
strtok is stateful and non-reentrant; it can only tokenize one string at a time. In your original code, you are trying to tokenize along two inputs at once, but it can't do that; it's only tokenizing along the last non-NULL string argument provided, which was linePointer.
To make this work, you need to use strtok_r which allows you to save the progress on each string without overwriting the progress on the other string.
I'm going through an introduction to programming in C book and I'm not sure what a line of code is doing. I run the code with and without this statement, the result is the same. I suspect that it is getting rid of the 'Enter' after the user inputs a string, but am not sure. I'm assuming the authors wrote this line for a reason. The character array is passed to a function that loops through an array of arrays and finds the text to search for using the 'strstr function.
int main () {
char search_for[80];
printf("Enter text to search for: ");
scanf("%79s", search_for);
search_for[strlen(search_for) -1] = '\0'; // why is this line here?
find_track(search_for);
return 0;
}
It overwrites the last character of the the scanned string with a null character, shortening the string by one character. Why the programmer wanted to do such a thing I can't say.
It's a mistake. That code was written to be used with fgets not scanf. Fgets adds a newline char to the end of the string the user enters and "search_for[strlen(search_for) -1] = '\0' " would overwrite that newline char with '\0'.
The problem: The code above with scanf causes the last character the user enters to be overwritten. So, a search using the string "ab" would only search for "a".
The program will work properly if "search_for[strlen(search_for) -1] = '\0'" is not included.
Delete one character from the end of the "string" search_for.
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.
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.