473,405 Members | 2,379 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,405 software developers and data experts.

check content of char array

7
hai guys....i have question about array...let say i have a filename newfile.txt..i want to check the file extension...if the extension is .txt then i'll store it otherwise i'll remove...i already have an idea...i want to check character by character...do someone have an example for this?
Jul 16 '07 #1
3 4669
svlsr2000
181 Expert 100+
hai guys....i have question about array...let say i have a filename newfile.txt..i want to check the file extension...if the extension is .txt then i'll store it otherwise i'll remove...i already have an idea...i want to check character by character...do someone have an example for this?
instead of checking character by character you can use strstr

char * a = "Newtext.txt";
char * ptr = strstr(a,".txt");

strstr returns valid pointer if it finds the substring.
Jul 16 '07 #2
Checking it character by character is not necessary when you already have a function like strstr() to do the job of searching a string in an array.

However if you need to do it character by character, you can try it this way, you could try it this way ... this is the longer version, you could use loop within a loop...

i = 0
while (str[i]!= '\0')
{
if (str[i] == '.')
{
//asuming that after filename.txt its end of string ie '\0' character)
if (str[i+1] == 't' && str[i+2] == 'x' && str[i+3] == 't' && str[i+4] == '\0')
{
printf("\ntxt extension found");
break;
}
i++; //so that next character can be checked.
}

Hope this helps
hai guys....i have question about array...let say i have a filename newfile.txt..i want to check the file extension...if the extension is .txt then i'll store it otherwise i'll remove...i already have an idea...i want to check character by character...do someone have an example for this?
Jul 16 '07 #3
zalina
7
instead of checking character by character you can use strstr

char * a = "Newtext.txt";
char * ptr = strstr(a,".txt");

strstr returns valid pointer if it finds the substring.
i've tried...but nothing happen...this function include in c or not?
Jul 17 '07 #4

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

Similar topics

4
by: JC | last post by:
hi i want to check a char in the char array public void characters(char chars, int start, int length) { if (chars!='/n' || chars((char)'/r')) { System.out.println("String read is " + new...
8
by: ali | last post by:
Hi, I'm having a problem understanding the reason for output on the following code: #include <iostream.h> int main() {
3
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; ...
7
by: Neo | last post by:
Hi, I have a programs that reads char ouput and should send a signal when it reads "login". I am currently reading into a char array but how do I check for the match? I don't think I can do- char...
3
by: NK | last post by:
char *a="nnnnnnn fgjfjgjf"; char *b="ABC jjjjjjjjjhhhh"; to swap these two string...... this swap fn is working template<class T> void Swap( const T *&a,const T *&b)//no need to write...
24
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is...
4
by: Rizladonovich | last post by:
I am currently reading/learning c. I often like to look at code while I read, - to see more practical use then often are feasible in educational books. I am looking at some code from wget. Code...
2
by: Lohith r | last post by:
Hi Friends, I have a serious and irritating problem, please help mdContext->digest is an unsigned char Array with hexadecimal values so for (i = 0; i < 16; i++) printf ("%02x",...
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: 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:
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.