473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

strings

if (line[strlen(line)-1]=='\n')
{
line[strlen(line)-1]='\0';
}

If anyone expalin how this code works? why 1 is deducted ?

thanks
Nov 14 '09 #1
6 1230
Markus
6,050 Expert 4TB
In C, array indexes start at 0. If strlen() returns 15 on an array that has was declared to have 14 elements, accessing array[strlen(array)] would evaluate to array[15]. Of course, as you can see this means we are going outside the bounds of the array (because the array index starts at 0). If we were to do array[strlen(array) - 1], we would then be accessing the last element of the array.

Mark.

P.S. Please see the posting guidelines on how to ask a question, specifically the part on using [code] tags.
Nov 14 '09 #2
Hello Mark,

Thanks for replly. But still confused as I'm not getting but will happen if we pass "Hello" as a strings and if we pass "Hello World" in an array of arr[10].

Thanks,
Nov 14 '09 #3
In the following program what is the purpose of defininig these two lines code?

if (line[strlen(line)-1]=='\n')
{
line[strlen(line)-1]='\0';
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int numParms, char *parms[])
{
const int LINE_SIZE = 1000;
const int STRING_SIZE = 100;
const int NUM_STRINGS = 100;
FILE *infile;
char strings[NUM_STRINGS][STRING_SIZE];
char line[LINE_SIZE];
char* result;
int size;
int count;
size = 0;
infile = fopen("in.txt", "r");
result = fgets(line, LINE_SIZE, infile);
while (result != NULL)
{
if (line[strlen(line)-1]=='\n')
{
line[strlen(line)-1]='\0';
}
if ((size>=NUM_STRINGS) || (strlen(line)>=STRING_SIZE))
{
fprintf(stderr, "\nRan out of memory.\n");
exit(EXIT_FAILURE);
}
strcpy(strings[size], line);
printf("<%s>\n", line);
result = fgets(line, LINE_SIZE, infile);
size++;
}
fclose(infile);
printf("\n");
for (count=0; count<size; count++)
{
printf("<%s>\n", (char*) strings[count]);
}
printf("\nAll done\n");
return 0;
}

Thanks.
Nov 14 '09 #4
Markus
6,050 Expert 4TB
@smartset
Try it and see for yourself - experimentation is a good way to learn and understand things (though I should note this was not your original question).
Nov 14 '09 #5
@smartset
If you create a array of 10 elements, you will have 10 positions, but the first one is 0, so you will have an array from [0] to [9], thats 10 elements.

If you build a "Hello" string(lets say called st) it will be like this:

Expand|Select|Wrap|Line Numbers
  1. st[0] = 'H';
  2. st[1] = 'e';
  3. st[2] = 'l';
  4. st[3] = 'l';
  5. st[4] = 'o';
  6. st[5] = '\0';
  7.  
This array could have a minimum of 6 elements..
strlen would return 5, so the '\0' is st[strlen(st)], so i don't think that -1 should be there.. but i can be wrong..

the '/0' char represents the end of the string. '\n' means a change of line (enter button). So that code is reading lines of the file you specified, those end with a '\n', because you are using fgets, so that will return a line of the file, putting that string in a char array, and changing the '\n' to a end of string.
Nov 15 '09 #6
Thanks. Now I got a clear idea of this code.
:)
Nov 15 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.