如何将字符串解析为单个字符? (真的很简单!)真的! [英] How do I parse a string into individual characters? (really simple!) really!

查看:70
本文介绍了如何将字符串解析为单个字符? (真的很简单!)真的!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello group!


我在欧洲,带着我的笔记本电脑旅行,我不是任何编译器

而不是Borland C ++ 5.5。可用。我也没有任何手册

或可用的帮助文件。可悲的是,更严重的问题!我从来没有使用过很多,所以,我不记得命令或功能了。我的问题

很简单。我发布了DOS窗口输出,所以你可以看到我正在使用的Borland

C ++版本:


E:\ TZ> bcc32 file2

Borland C ++ 5.5 for Win32版权所有(c)1993,2000 Borland

File2.cpp:

Turbo Incremental Link 5.00 Copyright(c)1997,2000 Borland


解析字符串所需的命令或函数是什么?

个别字符?


示例:


#include< stdlib.h>

#include< stdio.h>

#include< time.h>

#include< iostream.h>

int main(void)

{char MyString [80];

printf("输入字符串:");

得到(MyString);

printf("字符串输入为:%s \ n",MyString);

返回0;

}


如果我从键盘输入字符串为:早上好!

我想要一个变量,也许是一个矢量,比如

vec [1] = G,vec [2] = o,vec [3] = o,... vec [12] = g,vec [13] =!


我会需要确定字符串的长度(以字节为单位),然后

将字符串移动到最后。如何声明一个名为vec的字符

向量变量在这里,确定长度?然后我将

循环直到结束。


万分感谢!


Jeannie

解决方案



" Jeannie" <做** @ orPepper.com>在消息中写道

news:kk ******************************** @ 4ax.com ...

Hello group!

我在欧洲,带着我的笔记本电脑旅行,除了Borland C ++,我不需要任何编译器5.5。可用。我也没有任何手册
或帮助文件可用。可悲的是,更严重的问题!我从来没有使用过很多C ++,所以,我不记得命令或功能。我的问题很简单。我发布了DOS窗口输出,所以你可以看到我正在使用的Borland
C ++版本:

E:\ TZ> bcc32 file2
Borland C ++ 5.5 for Win32版权所有(c )1993年,2000年Borland
File2.cpp:
Turbo增量链接5.00版权所有(c)1997,2000 Borland

将字符串解析为什么所需的命令或函数是什么个人角色?

例如:

#include< stdlib.h>
#include< stdio.h>
#include< time.h>
#include< iostream.h>


应该是< iostream>

int main(无效)
{char MyString [80];
printf("输入一个字符串:");
获取(MyString);
printf("字符串输入为:%s \ n",MyString);
返回0;
}

如果我从键盘输入一个字符串:早上好!
我想要一个变量,也许是一个矢量,例如
vec [1] = G, vec [2] = o,vec [3] = o,... vec [12] = g,vec [13] =!


在C ++中,数组是从零开始的,你从vec [0]开始。

我需要确定字符串的长度,以字节为单位,然后
通过字符串移动到最后。如何声明一个名为vec的字符
向量变量在这里,确定长度?然后我就可以循环直到结束。

万分感谢!

Jeannie




无论如何,有标准库可以为您完成所有这些:


#include< iostream>

#include< string>


使用命名空间std;


string s;

getline(cin,s);


//现在如果你输入好的话然后

// s [0] =''G'',s [1] =''o'',s [2] =''o'',s [3] =' 'd''


Ben


Jeannie写道:

你好组!

我在欧洲,用我的笔记本电脑旅行,除了Borland C ++ 5.5之外,我不需要任何编译器。可用。我也没有任何手册
或帮助文件可用。可悲的是,更严重的问题!我从来没有使用过很多C ++,所以,我不记得命令或功能。我的问题很简单。我发布了DOS窗口输出,所以你可以看到我正在使用的Borland
C ++版本:

E:\ TZ> bcc32 file2
Borland C ++ 5.5 for Win32版权所有(c )1993年,2000年Borland
File2.cpp:
Turbo增量链接5.00版权所有(c)1997,2000 Borland

将字符串解析为什么所需的命令或函数是什么个人角色?

例如:

#include< stdlib.h>
#include< stdio.h>
#include< time.h>
#include< iostream.h>
int main(void)
{char MyString [80];
printf("输入一个字符串:");
获取(MyString);
printf("字符串输入为:%s \ n",MyString);
返回0;
}

如果我从键盘输入一个字符串:早上好!
我想要一个变量,也许是一个矢量,例如
vec [1] = G, vec [2] = o,vec [3] = o,... vec [12] = g,vec [13] =!

我需要确定长度以字节为单位的字符串,然后
通过字符串移动到最后。如何声明一个名为vec的字符
向量变量在这里,确定长度?然后我可以循环直到结束。



这不是一个做你想要的好方法。除了找到字符串的长度之外,你还想用这个向量做什么?
呢?


我的建议是:

#include< string>

#include< iostream>


//少输入例如

使用命名空间std;


int main()

{

string MyString;


getline(cin,MyString);


cout<< 长度= << MyString.length()<< ''\ n'';


for(int i = 0; i< MyString.length(); i ++)

cout<< ; " MyString的[" << i<< , << MyString [i]<< ''\ n'';


返回0;

}


你好


长度= 5

MyString [0] h

MyString [1] e

MyString [2] l

MyString [3] l

MyString [4] o


Brian


On Tue,2005年8月23日20:24:26 +1000,benben < moc.liamtoh@hgnohneb

向后阅读>写道:


Jeannie <做** @ orPepper.com>在消息中写道
新闻:kk ******************************** @ 4ax.com .. < Helloquote class =post_quotes> Hello group!

我在欧洲,和我的笔记本电脑一起旅行,除了Borland C ++ 5.5之外,我不需要任何编译器。可用。我也没有任何手册
或帮助文件可用。可悲的是,更严重的问题!我从来没有使用过很多C ++,所以,我不记得命令或功能。我的问题很简单。我发布了DOS窗口输出,所以你可以看到我正在使用的Borland
C ++版本:

E:\ TZ> bcc32 file2
Borland C ++ 5.5 for Win32版权所有(c )1993年,2000年Borland
File2.cpp:
Turbo增量链接5.00版权所有(c)1997,2000 Borland

将字符串解析为什么所需的命令或函数是什么个人角色?

例如:

#include< stdlib.h>
#include< stdio.h>
#include< time.h>
#include< iostream.h>



应该是< iostream>

int main(void)
{char MyString [80];
printf("输入字符串:");
获取(MyString);
printf(" ;字符串输入为:%s \ n",MyString);
返回0;
}
如果我从键盘输入字符串为:早上好!
我想要一个变量,也许是一个向量,例如
vec [1] = G,vec [2] = o,vec [3] = o,... vec [12] = g,vec [13] =!



在C ++中,数组是从零开始的,你从vec [0]开始。


我需要确定字符串的长度(以字节为单位),然后
将字符串移动到最后。如何声明一个名为vec的字符
向量变量在这里,确定长度?然后我就可以循环直到结束。

万分感谢!

Jeannie



无论如何,那里'这是为你做所有这些的标准库:

#include< iostream>
#include< string>

使用命名空间std;

字符串s;
getline(cin,s);

//现在如果输入Good,然后
// s [0] =''G'',s [1] =''o'',s [2] =''o'',s [3] =''d''



Ben,这看起来很棒,谢谢!


Hello group!

I''m in Europe, traveling with my laptop, and I don''t any compilers
other than Borland C++ 5.5. available. I also don''t have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don''t remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it''s
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie

解决方案


"Jeannie" <Do**@orPepper.com> wrote in message
news:kk********************************@4ax.com...

Hello group!

I''m in Europe, traveling with my laptop, and I don''t any compilers
other than Borland C++ 5.5. available. I also don''t have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don''t remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it''s
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
Should be <iostream>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!
In C++, arrays are zero-based, you start with vec[0].

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie



Whatever, there''s the standard library to do all these for you:

#include <iostream>
#include <string>

using namespace std;

string s;
getline(cin, s);

// now if you input "Good" then
// s[0]=''G'', s[1]=''o'', s[2]=''o'', s[3]=''d''

Ben


Jeannie wrote:

Hello group!

I''m in Europe, traveling with my laptop, and I don''t any compilers
other than Borland C++ 5.5. available. I also don''t have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don''t remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it''s
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.


That''s not a good way to do what you want. What exactly do you want to
do with this vector besides find the length of the string?

My suggestion would be:

#include <string>
#include <iostream>

// less typing for example
using namespace std;

int main()
{
string MyString;

getline(cin, MyString);

cout << "Length = " << MyString.length() << ''\n'';

for (int i = 0; i < MyString.length(); i++)
cout << "MyString[" << i << "] " << MyString[i] << ''\n'';

return 0;
}

hello

Length = 5
MyString[0] h
MyString[1] e
MyString[2] l
MyString[3] l
MyString[4] o


Brian


On Tue, 23 Aug 2005 20:24:26 +1000, "benben" <moc.liamtoh@hgnohneb
read backward> wrote:


"Jeannie" <Do**@orPepper.com> wrote in message
news:kk********************************@4ax.com.. .

Hello group!

I''m in Europe, traveling with my laptop, and I don''t any compilers
other than Borland C++ 5.5. available. I also don''t have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don''t remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it''s
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>



Should be <iostream>

int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!



In C++, arrays are zero-based, you start with vec[0].


I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie



Whatever, there''s the standard library to do all these for you:

#include <iostream>
#include <string>

using namespace std;

string s;
getline(cin, s);

// now if you input "Good" then
// s[0]=''G'', s[1]=''o'', s[2]=''o'', s[3]=''d''

Ben


Ben, this looks great, thanks!


这篇关于如何将字符串解析为单个字符? (真的很简单!)真的!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆