C ++字符串使用strtok,strlen和strcat [英] C++ string use strtok, strlen, and strcat

查看:51
本文介绍了C ++字符串使用strtok,strlen和strcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您需要编写一个程序,将来自用户的电话号码作为输入并将其存储为字符串值。电话号码由国家代码,城市代码和实际的7位数字组成,用? - ?分隔。用户可以按任何顺序输入电话号码。您的程序应该能够识别国家代码,城市代码和7位数字,并以下列格式显示。

国家代码 - 城市代码? 7位数



详细说明:


1.将电话号码作为用户输入。

2.该数字应存储为字符串值。

3.用户可以按任何顺序输入电话号码,例如0092-333-1234567或333-0092-1234567或1234567-333-0092等。

4。程序应该能够从字符串中识别国家代码,城市代码和7位数字,并以正确的顺序显示它。


样本输出1

输入完整的电话号码:0092-1234567-333


国家代码= 0092

城市代码= 333

7位数是= 1234567

电话号码的正确顺序是= 0092-333-1234567



样品输出2

输入完整的电话号码:1234567-321-0092


国家代码是= 0092

城市代码是= 321

7位数字= 1234567

正确顺序的电话号码是= 0092-321-1234567


样本输出3

输入完整的电话号码:300-0092-9876543


国家代码= 0092

城市代码= 300

7位数字= 9876543

电话号码的顺序是= 0092-300-9876543

You are required to write a program that takes a phone number as input from the user and stores it as a string value. The phone number will consist of country code, city code and actual 7-digit number separated by ?-?. User can enter the phone number in any order. Your program should be able to recognize the country code, city code and 7-digit number and display it in the following format.
Country code - City code ? 7-digit number


Detailed Description:

1. Take phone number as input from the user.
2. The number should be stored as a string value.
3. User can enter the phone number in any order for example 0092-333-1234567 or 333-0092-1234567 or 1234567-333-0092 etc.
4. Program should be able to recognize country code, city code and 7-digit number from the string and display it in the right sequence.


Sample Output 1
Enter the complete phone number : 0092-1234567-333

Country code is = 0092
City code is = 333
7-digit number is = 1234567
Phone number in correct sequence is = 0092-333-1234567



Sample Output 2
Enter the complete phone number : 1234567-321-0092

Country code is = 0092
City code is = 321
7-digit number is = 1234567
Phone number in correct sequence is = 0092-321-1234567


Sample Output 3
Enter the complete phone number : 300-0092-9876543

Country code is = 0092
City code is = 300
7-digit number is = 9876543
Phone number in correct sequence is = 0092-300-9876543

推荐答案

你试图这样做的目的是什么?


另外,既然你使用的是C ++,那么使用istringstream分割出来会更好字段而不是传统的C函数。
And where is your attempt to do this?

Also since you are using C++ you would be a lot better off using a istringstream to split out the fields rather than legacy C functions.


给我解决方案;;;;;;;;;;;;;;;;;;;,,,,,,
give me solution ;;;;;;;;;;;;;;;;;;.....


你一定是在开玩笑。

好​​的,笑话结束了。向我们展示你的代码中你被困住的部分,并仔细描述你遇到的问题。
You must be kidding.
OK, joke''s over. Show us the portion of your code where you''re stuck and carefully describe the problem you''re having with it.


这篇关于C ++字符串使用strtok,strlen和strcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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